Process.Start can not open a file based on the extension

267 Views Asked by At

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.

1

There are 1 best solutions below

4
user246821 On BEST ANSWER

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: