If I run the following command in a command prompt, it works:
"C:\Program Files (x86)\AppFolder\do.exe"
If I try to run the same thing as a process:
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c ""C:\Program Files (x86)\AppFolder\do.exe""";
using (Process process = Process.Start(startInfo))
{
...
}
It does not appear to run. If I look at the startInfo.Arguments, it appears as follows:
/c \"C:\\Program Files (x86)\\AppFolder\\do.exe\"
What am I missing? Can you not pass arguments that have quotes or spaces? I see a lot of examples where people are passing items with spaces/quotes in just fine, but following them just doesn't seem to work. This is the narrowest example I can think of.
EDIT: If I just create a shortcut so I don't use spaces or quotes, it works:
startInfo.Arguments = #"/c D:\_test\Do.lnk";
I do not use '#'. This is correct implementation:
startInfo.Arguments = "/c \"C:\\Program Files (x86)\\AppFolder\\do.exe\"";
Related
i want to open the prompt command and execute arguments so i'm using this code :
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 = #"cd\";
process.StartInfo = startInfo;
process.Start();
But the arguments doesn't works and the prompt command open on bin/debug folder instead of executing cd\ and open on c:/
tried with :
System.Diagnostics.Process.Start("cmd.exe", #"cd\");
but didnt work either
(cd\ is just an example to see if it work the final command i need to execute is cd/ cd C:\Program Files (x86)\ffmpeg ffmpeg32 -i C:\Users\Oxitroy\Documents\instaJanvier1.mp4)
You need to add /c to the command:
System.Diagnostics.Process.Start("cmd.exe", #" /c cd\");
/c : Carries out the command specified by string and then terminates.
But try something a little more lengthy, so you can see if anything happens.
i am performing offline operations on images taking image as a input with parameters and processing it in VTK C++ exe i am unable to pass parameters to C++ exe through C# program and retrive output .
please explain me with some example
If you just mean that you have a compiled C++ program (which we'll call "foo.exe", with path stored in string "exe_folder") and you want to call that with command line parameters (stored in string "exe_params") from C#, then the following should work:
string exe_params = "target_image.jpeg HOUGH_TRANSFORM"; // Or whatever params are appropriate.
string exe_full_path = Path.Combine(exe_folder, "foo.exe");
Process proc = System.Diagnostics.Process.Start(exe_full_path, exe_params);
https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx
Let say that your executable is called test.exe and it is in the test dir. For me, the following would be working:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C cd C:\\..test\\ && test.exe target_image.jpg yourtransformation";
process.StartInfo = startInfo;
process.Start();
If you have further problems, try setting the working directory of processStartInfo.
I am trying to run batch commands in a C# application.
Usually, I would do this through the following code:
string command = "shutdown -s -t 120";
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = ("/c" + command);
process.StartInfo = startInfo;
process.Start();
However, I am building said application for a network that doesn't allow CMD.EXE. I can gain access to the Command Prompt through making a *.bat file with the "COMMAND.COM" string in it - then I have to type in the commands manually. The above code will not allow me to pass string commands to a batch file, only an *.exe file. Is there any way around this?
The answer is to bypass cmd altogether, you don't need it here, shutdown will be a process itself, so just run it directly:
Process.Start("shutdown","/s /t 120");
Shutdown isn't a batch command, it's a system executable. You can call it instead of cmd:
C:\Windows>dir /s shutdown.exe
Volume in drive C has no label.
Volume Serial Number is 008A-AC5B
Directory of C:\Windows\System32
30-10-2015 08:17 37.376 shutdown.exe
1 File(s) 37.376 bytes
Directory of C:\Windows\SysWOW64
30-10-2015 08:18 33.792 shutdown.exe
1 File(s) 33.792 bytes
So you can replace your current code with:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = ("-s -t 120");
process.StartInfo = startInfo;
process.Start();
I want to compile an .asm file placed in bin folder in the masm through c#.I have tried multiple methods like process.start but nothing helps.It opens the cmd but the command "ml" never executes.It either open the pwb.exe(MASM) or the 'file.asm' in notepad.I give these arguments to CMD "path\ml file.asm" which works fine manually.ml is a command used to compile .asm files. One of the method I used is following
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\WINDOWS\system32\cmd.exe";
startInfo.Arguments = "C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml file.asm";
process.StartInfo = startInfo;
process.Start();
If you want to start the process in this way, you'll need to put quotes round the path, due to the spaces:
startInfo.Arguments = #"""C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml"" file.asm";
(In a verbatim string literal, you include double-quotes by doubling them.)
Alternatively, if ml is actually an executable (I know nothing about masm) you could just use:
startInfo.FileName = #"C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml.exe";
startInfo.Arguments = "file.asm";
To start "cmd.exe" and run another program you need to use the /c switch.
/C Carries out the command specified by string and then terminates
So you would need:
startInfo.Arguments = "/c \"C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml.exe\" file.asm";
I've added extra quotes for the space in the path.
If you don't need to run it via the command prompt - "cmd.exe" - then you can put the path to "ml.exe" in FileName and just pass the "file.ml" as the Arguments.
I am trying to delete a directory using C#. The first method I tried was
Directory.Delete(#"C:\Program Files (x86)\Qmuzki32");
I get an exception stating that the directory is not empty. I then found a cmd command which I can use to delete the directory quietly regardless of the fact that the directory is empty or not. I ran the following command in cmd:
rmdir /s /q "C:/Program Files (x86)/Qmuzik32"
This worked and did exactly what I wanted it to do. With my first attempt I tried building this command into a C# process like so:
if (Directory.Exists(#"C:\Program Files (x86)\Qmuzik32"))
{
string sQM32Folder = #"C:\Program Files (x86)\Qmuzik32";
Process del = new Process();
del.StartInfo.FileName = "cmd.exe";
del.StartInfo.Arguments = string.Format("rmdir /s /q \"{0}\"", sQM32Folder);
del.WaitForExit();
}
This did not work and then I tried it like this:
if (Directory.Exists(#"C:\Program Files (x86)\Qmuzik32"))
{
string sQM32Folder = #"C:\Program Files (x86)\Qmuzik32";
Process del = new Process();
del.StartInfo.FileName = "rmdir.exe";
del.StartInfo.Arguments = string.Format("/s /q \"{0}\"", sQM32Folder);
del.WaitForExit();
}
Same problem. I get the exception:
No process is associated with this object.
I do think I am on the right track; maybe the code above just requires some tweaking.
Just use Directory.Delete(string, bool).
While the low-level filesystem APIs of course require you to make sure the directory is empty first, any half-decent framework abstracting them allows you do do a recursive delete. In fact, existence of such a method would be the first thing I'd check before even trying to resort to external programs.
If you want to use the cmd way you can use this:
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C rd /s /q \"C:\\Program Files (x86)\\Qmuzik32\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
del.Start();
del.WaitForExit();
you didn't start the procces so it doesn't have a PID so it dies