Kill process started with System.Diagnostic.Process.Start(“FileName”)

后端 未结 6 1337
生来不讨喜
生来不讨喜 2021-02-05 22:48

I am trying to create an app that will perform actions on specific times (much like the Windows Task Scheduler). I am currently using Process.Start() to launch the file (or exe)

相关标签:
6条回答
  • 2021-02-05 22:56

    Don't do it this way.

    It's not clear whether the intent of your program is 'Always launch with Windows Media Player' or 'Launch with the registered MP3 player', which might be, say, iTunes.

    If you need WMP, use Process.Start with the full path to windows media player.

    If you need the registed MP3 player, you can find out the correct exe using the code shown here. Again, start the process with this exe path, passing the MP3 as a parameter.

    0 讨论(0)
  • 2021-02-05 23:00

    Process.Start(string,string) returns you a Process resource that you can use to further control the new process.

    Process newProcess = Process.Start("param1", "param2");
    if (newProcess != null && !newProcess.HasExited)
      newProcess.Kill();
    

    The same structure works if you use Process.Start(string), or any other static Process.Start overload.

    Process.Start() is a member function and associates a new or reused Process with the Process component identified by this. Behaviour of this method depends on the properties of the Process identified by this.

    0 讨论(0)
  • 2021-02-05 23:01

    I tried to open .txt file and the process of my text editor was returned, also I tried .mp3 by WMP and it returned null. So it depends on the application. Do you need to run you mp3 only with WMP? If not, you definitely can create the application which will return the Process object.

    0 讨论(0)
  • 2021-02-05 23:14

    If you are letting the registered windows program to open the file, rather than picking the program you want. Then I advise you do not kill the process.

    The reason for this is what say your program does use the default application, but that application is already in use, and contains unsaved data. A user would not be happy for your program to overtake there application with the new file and then kill off the process that was already in use by the user for another purpose. Sure, it might not be in use but you must consider the worst case.

    As such, I recommend what has been suggested. use Process.Start() with the full path to the program to be used and the file to be opened.

    0 讨论(0)
  • 2021-02-05 23:16

    proc = Process.Start(filename) should work, but like you say, it returns null instead of a process.

    That seems to be inherent to Windows Media Player. Other applications return the process. You can get Windows Media Player's process by specifying the application in the start method.

    proc = Process.Start("C:\Program Files\Windows Media Player\wmplayer.exe", filename)
    

    Then you can kill it normally.

    proc.Kill()
    

    You will probably need to get the location of the application assiciated with .mp3 files from the registry.

    0 讨论(0)
  • 2021-02-05 23:19

    Two ways:

    1-

    Process customProc = Process.Start("ExecutablePath", "Argument(s)");  
    customProc.Kill()
    

    2-

    Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProcessName")  
    For Each p As Process In pProcess
    p.Kill()
    Next
    
    0 讨论(0)
提交回复
热议问题