Process.Start 无法根据文件扩展名打开文件。

huangapple go评论63阅读模式
英文:

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

问题

如果我在命令提示符中输入以下内容:

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

那么相应的文件将会用默认应用程序打开。我可以从程序中执行相同的操作:

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

不幸的是,这不再起作用了。我使用的是Windows 10和.NET 7。

Process.Start("notepad.exe", @"c:\Data\c.txt"); // 起作用
Process.Start("excel.exe", @"c:\Data\a.xls"); // 不起作用

如果我提供excel.exe的完整路径,那么它可以工作。我想实现以前的功能,只需提供文件名,然后用默认应用程序打开它。

英文:

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

得分: 3

UseShellExecute 属性设置为 true。

> 在 .NET Framework 应用中,默认值为 true,在 .NET Core 应用中默认值为 false。

另请参阅 StartInfo

下载/安装 NuGet 包: System.Diagnostics.Process

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

附加参考资料

英文:

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:

huangapple
  • 本文由 发表于 2023年2月16日 04:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定