I am using below codes with c# for executing doss command in the comment line i executed notepad and in the comment line i tried to execute excel but nobody running if i comment any one from notepad or excel then it executes. I want to execute dos commands one by one weather previous command finish its process or not.
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 = "/C notepad";
startInfo.Arguments = "/C excel";
process.StartInfo = startInfo;
process.Start();
By setting the Arguments property twice you are replacing "/C notepad" with "/C excel". The process is launching cmd.exe and passing only "/C excel" to it as an argument.
I suspect that cmd.exe cannot find the Excel exe when it's passed as a "/C" argument. This would explain why you don't see anything executing. You may need to specify the complete path to Excel.
If you want to execute both Notepad and Excel, you will need to launch them one after another by first setting one argument then calling Start for each application.
Something like this:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C notepad";
process.Start();
startInfo.Arguments = "/C {Insert the full path to Excel exe}excel";
process.Start();
Alternatively, you can launch both applications launched from the process rather then as parameters to cmd.exe. Process itself is cmd.exe. This will allow the OS to have a better chance of finding the application using the Windows Path variables
Something like this:
System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo();
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = startInfo;
startInfo.FileName = "notepad";
process.Start();
startInfo.FileName = "excel";
process.Start();
Related
I need to copy a file from one directory to another and do something with that file. I need to copy it with cmd, rather than File.Copy(), because I need the copy to be done as a part of ProcessStartInfo.
You can use this code and change startInfo.Arguments, but /C should be!
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 = "/C copy example.txt backup.txt";
process.StartInfo = startInfo;
process.Start();
You can create a bat-file to copy one or multiple files (using *). Then execute the batch file.
string batFileName = #"C:\{filePath}\copy.bat";
System.IO.File.WriteAllText(batFileName, #"copy {fileName}.{extension} {destination-filePath}");
System.Diagnostics.Process.Start(batFileName);
I was able to formulate this answer using the DOS Copy syntax along with this Stack Overflow QA
Start cmd window and run commands inside
var startInfo = new ProcessStartInfo {
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(#"copy c:\Source\Original.ext D:\Dest\Copy.ext");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
I was running some exe with ProcessStartInfo
ProcessStartInfo startInfo = new ProcessStartInfo("my.exe");
startInfo.Arguments = "foo.txt bar.txt";
startInfo.WorkingDirectory = path;
Process process = Process.Start(startInfo);
process.WaitForExit();
It was working fine on my PC, but when I move the code to another PC, the same code keep hanging.
After some trying, I was able to fix it by setting UseShellExecute to false (and use absolute path for the exe filename).
ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(Server.MapPath(#"~\mypath"),"my.exe"));
startInfo.Arguments = "foo.txt bar.txt";
startInfo.UseShellExecute = false;
Process process = Process.Start(startInfo);
process.WaitForExit();
The question is why? Why does using shell hang the process? What is the difference between using or not using shell to execute?
Thanks for the help!
I'm trying to use gcc compiler to get Output exe file. When I perform this from file explorer and cmd manually it work as expected but through Code no output is generated. I'm new to this C# and any help would be really appreciated.
Process process = new Process()
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo.WorkingDirectory = #"C:\Users\abc\Documents\Visual Studio 2017\Projects\CodeJam_FormApplication\CodeJam_FormApplication";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C gcc Test.c -o Output.exe";
process.StartInfo = startInfo;
process.Start();
I want to open IE (it doesn't matter the web site it will open with), but I found out that using this code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "http://www.google.com";
proc.Start();
will start the browser as a current user, but with this code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
it wouldn't, why is that? what is the reason?
Wrap the code in a try catch and take a look at the exception being raised, that should help find out why it is not working.
I have a small problem. Okay let's say from my C# console application I want to run a batch file that will take an argument. The string variable at the stop of my C# application will be the string argument to pass to the batch file. How would I go about doing it?
Here is my code so far my C# console program:
//String argument to pass to the batch file
string message = "Hello World";
System.Diagnostics.Process process = new System.Diagnostics.Process();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Greetings.bat";
startInfo.Arguments = "/C " + message;
process.StartInfo = startInfo;
process.Start();
My Batch File
CLS
#ECHO OFF
ECHO %1
You can give argument like this.
ProcessStartInfo psi = new ProcessStartInfo(filePath);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.Arguments = "value1";