How do I launch files in C#

后端 未结 3 1557
感动是毒
感动是毒 2020-11-30 03:06

-Edit- I feel like an idiot. I had a feeling something like the answer below would work but didnt see any google results similar to the answers below. So when i saw this com

相关标签:
3条回答
  • 2020-11-30 03:46

    Assuming that you just want to launch files which already have some associated applications (eg: *.txt is associated with notepad), Use System.Diagnostics.Process.

    e.g. :

     using System.Diagnostics;
        Process p = new Process();
        ProcessStartInfo pi = new ProcessStartInfo();
        pi.UseShellExecute = true;
        pi.FileName = @"MY_FILE_WITH_FULL_PATH.jpg";
        p.StartInfo = pi;
    
        try
        {
            p.Start();
        }
        catch (Exception Ex)
        {
            //MessageBox.Show(Ex.Message);
        }
    

    Note: In my PC, the pic opens in Windows Picture & Fax Viewer since that is the default application for *.jpg files.

    0 讨论(0)
  • 2020-11-30 03:55

    Use:

    System.Diagnostics.Process.Start(filePath);
    

    It will use the default program that would be opened as if you just clicked on it. Admittedly it doesn't let you choose the program that will run... but assuming that you want to mimic the behaviour that would be used if the user were to double-click on the file, this should work just fine.

    0 讨论(0)
  • 2020-11-30 04:03

    It really sounds like you're looking more for this:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.EnableRaisingEvents = false;
    proc.StartInfo.FileName = "<whatever>";
    proc.Start();
    
    0 讨论(0)
提交回复
热议问题