Kill specific Excel process

1. KillSpecificExcelFileProcess(“example1.xlsx”);

2. private void KillSpecificExcelFileProcess(string excelFileName){

var processes = from p in Process.GetProcessesByName(“EXCEL”)
select p;

foreach (var process in processes){

if (process.MainWindowTitle.Contains(“Microsoft Excel – ” + excelFileName))

process.Kill();

}

}

Anuncios

Establish network connection

//開始網路連線
System.Diagnostics.ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo(@”C:\WINDOWS\system32\net.exe”);
psInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psInfo.Arguments = @”net use \\192.168.0.15\RD$ ams.prog /user:programw”; //輸入相關資訊
System.Diagnostics.Process.Start(psInfo);

Exit Excel Application Process

excelBook.Close();

excelApp.Quit();   //Important: When you forget this, Excel keeps running in the background.

Check if a process is opened

1st way:

foreach(Process clsProcess in Process.GetProcesses()){

if(clsProcess.ProcessName.Contains(processNameWithoutEXE)){

return true;

return false;

}

}

 

2nd way:

Process[] pname = Process.GetProcessesByName(“notepad”);

if (pname.Length == 0)

MessageBox.Show(“nothing”);

else

MessageBox.Show(“run”);

Kill a program

Process[] proc = Process.GetProcessByName(“mspaint”);

foreach(Process prs in proc){

prs.Kill();

}

OR

foreach(var process in Process.GetProcessByName(“paint”)){

process.Kill();

}

Running a program

System.Diagnostics.Process.Start(“mspaint.exe”);   //Only for program in System32

OR

ProcessStartInfo info = new ProcessStartInfo(@”C:\…exe”);

System.Diagnostics.Process.Start(info);

info.WorkingDirectory = @”C:\Windows\System32″;