locked
Issue on Process.Start() RRS feed

  • Question

  • Hello, 

    I have an issue with Process.Start() when I want to open a PDF file or a shorticut (lnk). Below my code:

    Process.start(@"C:\file1.pdf"); //it works
    
    string output = @"C:\file1.pdf";
    Process.start(path); // if fails
    
    string path = "\"" + output + "\"";
    Process.start(path); //it fails
    
    //lnk
    Process.start(@"C:\test.lnk"); //it works
    
    string output2 = @"C:\test.lnk";
    Process.start(output2); // if fails

    I don't understand why it works for one case and not for the other case.

    When I use a variable to stock the path, Process.Start() does not work.

    How to resolve it?

    Thanks.

    Sunday, July 26, 2020 8:27 PM

All replies

  • I'm not sure if you typed this into MSDN manually or copy/pasted from VS, but the Start() method of Process is capitalized.  Did you maybe inherit the Process class and try to extend it with your own lowercase start() method?

    Have you tried creating an actual instance of Process and then setting up the Process.StartInfo with the target filename, instead of just Process.Start()?

    I see also that you're trying to inject literal double-quotes into your path.  Is that because the real path contains spaces?  Have you tried:

    '"' + output + '"'

    ie making character literals instead of trying to escape in strings with backslashes?


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Sunday, July 26, 2020 9:30 PM

  • Hi speed780, 
    Based on your code, the Start() method of Process is capitalized what Andrew B. Painter has pointed.
    I made a test with your code and it worked fine. So what error did you encounter?
    I use the following code to create a shorcut.lnk:

    WshShell wsh = new WshShell();
    IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
    shortcut.Arguments = "";
    shortcut.TargetPath = "C:\\Test\\test.xlsx";
    shortcut.WindowStyle = 1;
    shortcut.Description = "my shortcut description";
    shortcut.WorkingDirectory = "C:\\Test";
    shortcut.IconLocation = "C:\\Test";
    shortcut.Save();

    Then made a test with your code

    Process.Start(@"C:\Users\Desktop\shorcut.lnk"); 
    string output2 = @"C:\Users\Desktop\shorcut.lnk";
    Process.Start(output2); 

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 27, 2020 6:09 AM
  • Hi speed780,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 30, 2020 3:02 AM