Closing a file using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nixboy
    New Member
    • Jun 2007
    • 4

    Closing a file using C#

    Hi can anyone tell me how to close an exe file using c#?
  • Atran
    Contributor
    • May 2007
    • 319

    #2
    Originally posted by nixboy
    Hi can anyone tell me how to close an exe file using c#?
    Hello, Welcome to thescripts.
    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:
    Code:
     if (s.CompareTo("taskmgr") == 0)
    taskmger is Task Manager process name, and make sure to dont write taskmgr.exe.
    Hope this help you.

    Comment

    • nixboy
      New Member
      • Jun 2007
      • 4

      #3
      it worked perfectly thanks.

      Comment

      • blackjack2150
        New Member
        • Feb 2007
        • 79

        #4
        Originally posted by nixboy
        it worked perfectly thanks.
        If the application you're tying to kill has a graphical interface you can alternately use the CloseMainWindow method of the Process class.

        Comment

        • Atran
          Contributor
          • May 2007
          • 319

          #5
          Originally posted by nixboy
          it worked perfectly thanks.
          You welcome........ .
          Thanks for enjoying the site.

          Comment

          Working...