c# Process exe with arguments gives error - c#

I sort of stuck into what should be a small issue. I need to run two different processes but the second failures.
My first process is:
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "xxxx.xxxx.cmd";
string arguments = "-Sxxxx -f -d --port xxxx > c:\\xxxx\\tmp\\bug.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
No worries here. Everything works but the second.
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "WebDaemon.exe";
string arguments = "-debug -i WebDaemon.xxxx.1234 --ServerPort=4444 --MultipleServerPorts=1 --CGIPort=4500 --CGICallbackPort=4600 --MinServerProcs=2 --MaxServerProcs=2 > c:\\xxxx\\tmp\\WebDaemonTrace.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
Gives an error. The process created and shows up in taskmanager, but after 5 seconds it disappears. Furthermore i can read in a debug message that:
"commandline argument '>' is unknown"
I cannot seem to find the problem. Open to any suggestions :-)

Related

How to run an exe file using C# in console application

I am trying to run an exe file in my console application which is located on a network drive.
So what needs to happen is the app needs to map the network drive with a drive letter by using this code:
private static void MapDrive()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "net.exe";
startInfo.Arguments = #"use w: \\<server>\CompanyData\W10 /user:Administrator Password";
process.StartInfo = startInfo;
process.Start();
}
This works great and the drive letter is mapped.
Now the problem I am facing is to run the exe file with in this mapped drive.
I have tried the below but does not seem to work:
private static void RunSetup()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"w:\setup.exe";
process.StartInfo = startInfo;
process.Start();;
}
Nothing seems to happen in regards to launching the exe file.
I need to know what I am doing wrong here?
Thanks
Either include the "/c" argument
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c w:\setup.exe";
or set FileName directly to the setup.exe
startInfo.FileName = "w:\setup.exe";
as mentioned in the comments

System.ComponentModel.Win32Exception c#

This is my code:
string damnfile = System.IO.Path.GetDirectoryName(choofdlog.FileName);
ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "-Scan -ScanType 3 -File" + damnfile;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
As you can see, I want to run WinDefender scan for a specific file, but it throws error that the process is not found, but this code works:
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "-a";
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
Also I want in the second code to redirect the cmd output to listBox, but thats the second problem, first help me with the defender scan please.
Solveded like now its opening, because I replaced
ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
with
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Program Files\\Windows Defender\\MpCmdRun.exe");
Now appeared another problem: even I passed some arguments, the windows immediately close.

DoxBox start from C#

I try to start DoxBox from my console application, but it simply do nothing. What can be a problem?
Process proc = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = "C:\\Program Files (x86)\\DoxBox";
startinfo.FileName = "cmd.exe";
string strComm = "DoxBox.exe /dismount H:";
startinfo.Arguments = strComm;
proc.StartInfo = startinfo;
proc.Start();
Why do you start the procces with cmd.exe?
It isn't in your C:\\Program Files (x86)\\DoxBox directory, I'm sure.
Provide the DoxBox file in the FileName, without cmd.exe at all:
Process proc = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = "C:\\Program Files (x86)\\DoxBox";
startinfo.FileName = "DoxBox.exe";
string strComm = "/dismount H:";
startinfo.Arguments = strComm;
proc.StartInfo = startinfo;
proc.Start();

How to launch a single process the pipes one output into another?

As the question states, I am trying to launch two processes from a single process call using c# process library.
So for example, I'd like to run something like:
application1 "arg1" "arg2" | application2 "arg1".
At the moment, I have it setup like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = SEEBELOW;
startInfo.FileName = FIRSTAPPLICATIONLOCATION;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
Where the process filename is "application1" and the arguments of "application1" are " "arg1" "arg2" | "application2" "arg1".

Command line run error

After looking on forums, I have written this snippet:
public string ExecuteCmd()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = this.m_command;
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
The m_command is member of a class, initialized in the constructor.
For my tests, it is net user. When the compiler arrives at this point, I get the following exception:
StandardOut has not been redirected or the process hasn't started yet.
Where is my mistake?
You need this:
//....
startInfo.Arguments = "/C " + this.m_command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//....

Categories