0

If I write into the command prompt

c:\Data\a.xls
c:\Data\b.pdf
c:\Data\c.txt

then the corresponding files are opened with the default application. I could do the same from program.

Process.Start(@"c:\Data\a.xls");
Process.Start(@"c:\Data\b.pdf");
Process.Start(@"c:\Data\c.txt");

Unfortunately, this does not work anymore. I use windows 10 and .net7.

Process.Start("notepad.exe", @"c:\Data\c.txt"); // works
Process.Start("excel.exe", @"c:\Data\a.xls"); // does not work

If I provide the full path of excel.exe then it works. I would like to achieve the old functionality just to provide the filename and open it with the default application.

Istvan Heckl
  • 864
  • 10
  • 22

1 Answers1

3

Set the UseShellExecute property to true.

The default is true on .NET Framework apps and false on .NET Core apps.

Also see StartInfo.


Download/install NuGet package: System.Diagnostics.Process

ProcessStartInfo startInfo = new ProcessStartInfo() { FileName= @"c:\Data\a.xls", UseShellExecute = true };
Process.Start(startInfo);

Additional References:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • Can you improve this answer by explaining what is going on here? _Why_ does using shell execute get around the problem the OP was facing? Why did that occur in the first place, i.e. what is happening or not happening that used to work? – rory.ap Feb 15 '23 at 21:13
  • @rory.ap: I think that is mentioned in the code block in my post. But I'll add some additional references in case someone would like to know more. – Tu deschizi eu inchid Feb 15 '23 at 22:56
  • @user09938 You should excerpt/summarize the relevant information from whatever references you have. Look, it's up to you, but it can be the difference between a good answer and a great answer. I found myself wondering about the details here, and maybe people don't feel like navigating to (or don't realize they will find that information easily at) linked sources. Linked sources are nice for people who wish to learn a whole lot about something, but SO answers should be self-sufficient and good on their own. – rory.ap Feb 15 '23 at 23:15
  • @rory.ap: That's about as much as I know about it. I'd have to conduct more research to know more about it. Unfortunately, I don't have the time for that at the moment. If you have additional information, feel free to provide it. You may consider adding another answer. – Tu deschizi eu inchid Feb 16 '23 at 00:39