Hi
Friends in a winform I have a button called uninstall .Upon clicking on this button I want to uninstall certain software say for example winamp and also I want to know how to end a process say spool.exe that is running. We can do this by clicking remove in the add/remove programs of control panel and clicking the end process button in the task manager but I want to do it in my program say form1.cs button click event?
plz help
thanks
Loading

Roei BarRank #1,271 · 1.4K reputationPosted Sep 5, 2009, 1:19 AM EDT
in order to kill a process here is a code snippet:
//Namespaces needed using System.Diagnostics; public bool FindAndKillProcess(string name) { //here we're going to get a list of all running processes on //the computer foreach (Process clsProcess in Process.GetProcesses()) { //now we're going to see if any of the running processes //match the currently running processes by using the StartsWith Method, //this prevents us from incluing the .EXE for the process we're looking for. //. Be sure to not //add the .exe to the name you provide, i.e: NOTEPAD, //not NOTEPAD.EXE or false is always returned even if //notepad is running if (clsProcess.ProcessName.StartsWith(name)) { //since we found the proccess we now need to use the //Kill Method to kill the process. Remember, if you have //the process running more than once, say IE open 4 //times the loop thr way it is now will close all 4, //if you want it to just close the first one it finds //then add a return; after the Kill clsProcess.Kill(); //process killed, return true return true; } } //process not found, return false return false; }in order to uninstall a program you can either do that by msi like this :
private void uninstallSoftware() { Process p = new Process(); p.StartInfo.FileName = "msiexec.exe"; p.StartInfo.Arguments = "/x \"C:\\Application.msi\"/qn"; p.Start(); }or by registry information, like this for
Microsoft.Win32.RegistryKey Fregistry = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE") .OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion") .OpenSubKey("Installer").OpenSubKey("UserData") .OpenSubKey("S-1-5-18").OpenSubKey("Products"); string []Names = Fregistry.GetSubKeyNames() ; string uninstall = ""; string ApplicationName = "Adobe Reader 7.0.8"; for (int i = 0; i < Names.Length; i++) { Microsoft.Win32.RegistryKey FTemp = Fregistry.OpenSubKey(Names[i]).OpenSubKey("InstallProperties"); if (FTemp.GetValue("DisplayName").ToString() == ApplicationName) { object obj = FTemp.GetValue("UninstallString"); if (obj == null) uninstall = ""; else uninstall = obj.ToString(); i = Names.Length; } } System.Console.WriteLine(uninstall); System.Diagnostics.Process FProcess = new System.Diagnostics.Process(); string temp = "/x{" + uninstall.Split("/".ToCharArray())[1].Split("I{".ToCharArray())[2]; //replacing with /x with /i would cause another popup of the application uninstall FProcess.StartInfo.FileName = uninstall.Split("/".ToCharArray())[0]; FProcess.StartInfo.Arguments = temp; FProcess.StartInfo.UseShellExecute = false; FProcess.Start(); System.Console.Read();