1.open a batch file
2.If it opened earlier then need not to open again.
3.If you close, it will open again.
using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
namespace runBatFile
{
public partial class _Default : System.Web.UI.Page
{
private static bool isOpen = false;
private static Process batProcess = null;
protected void Page_Load(object sender, EventArgs e)
{
string filepath = Server.MapPath("testBat.bat");
filepath = filepath.Replace(@"\", @"/");
//if (isOpen == false)
//{
// batProcess = Process.Start(filepath);
// batProcess. EnableRaisingEvents = true;
// // isOpen = true;
//}
//if (batProcess.HasExited)
//{
// batProcess = Process.Start(filepath);
//}
//if (IsAlreadyRunning(filepath) == false)
// batProcess = Process.Start(filepath);
if (isOpen == false)
{
isOpen = true;
try
{
batProcess = Process.Start(filepath);
batProcess.EnableRaisingEvents = true;
batProcess.Exited += new EventHandler(batProcess_ Exited);
}
catch (Exception ex)
{
// handle exception
}
}
}
private void batProcess_Exited(object sender, System.EventArgs e)
{
batProcess.Start();
}
private static bool IsAlreadyRunning(string strLoc)
{
//string strLoc //= Assembly.GetExecutingAssembly( ).Location;
FileSystemInfo fileInfo = new FileInfo(strLoc);
string sExeName = fileInfo.Name;
bool bCreatedNew;
Mutex mutex = new Mutex(true, "Global\\" + sExeName, out bCreatedNew);
if (bCreatedNew)
mutex.ReleaseMutex();
return !bCreatedNew;
}
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName. Contains(name))
{
return true;
}
}
return false;
}
}
}
No comments:
Post a Comment