How can I check through C# code, if MyBat.bat file is running or not in the system.
I have tried this but not working properly.Please suggest some other way.
Process proc = new Process();
proc.StartInfo.FileName = @"D:\MyBat.bat";
if (proc.Start())
{
Console.Write("This is bat files Open!.");
}
else
{
Console.Write("Welcome");
}
I only want to check MyBat.bat file is running in the system or not. Because .bat file run in Command Prompt so how can i verify that MyBat.bat file is running.
You should invoke your batch file via cmd.exe /c
, not directly.
so:
var command = "foo.bat";
Process p = new Process(){
StartInfo = new ProcessStartInfo("cmd.exe", "/c " + command)
};
var started = p.Start();
if(started)
{
Console.WriteLine("started");
var sync = true; //or false, see below
if(sync)
{
//either wait synchronously
p.WaitForExit();
Console.WriteLine("finished");
}
else
{
//or wait for an exit event asynchronously
p.Exited += (a, b) => { Console.WriteLine("finished"); }
}
}
else
{
Console.WriteLine("not started");
}
If you want to know right now, you can check the HasExited property.
var isRunning = !myProcess.HasExited;
If it's a quick process, just wait for it.
myProcess.WaitForExit();
If you're starting one up in the background, subscribe to the Exited event after setting EnableRaisingEvents to true.
myProcess.EnableRaisingEvents = true;
myProcess.Exited += (sender, e) => { MessageBox.Show("Process exited"); };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.