Hi can anyone tell me how to close an exe file using c#?
Closing a file using C#
Collapse
X
-
Hello, Welcome to thescripts.Originally posted by nixboyHi can anyone tell me how to close an exe file using c#?
To close the file, you must end (kill) the process of the file.
If any file (program) running, you can see its process in the TaskManager.
So in this code, we will end the file process (to close the running file).
-------------------------------------------------------------------------------Code:using System; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Process[] pArry = Process.GetProcesses(); foreach (Process process in pArry) { string s = process.ProcessName; s = s.ToLower(); if (s.CompareTo("yourProgramProcessName") == 0) { process.Kill(); Console.WriteLine("You've Done!"); } } Console.ReadKey(); } } }
Read this line:
Code:if (s.CompareTo("yourProgramProcessName") == 0)
Write the program process name, but do not write the program format name, read this line:
taskmger is Task Manager process name, and make sure to dont write taskmgr.exe.Code:if (s.CompareTo("taskmgr") == 0)
Hope this help you. -
If the application you're tying to kill has a graphical interface you can alternately use the CloseMainWindow method of the Process class.Originally posted by nixboyit worked perfectly thanks.Comment
Comment