You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用System.Diagnostics.Process.Start打开EML文件遇Win32Exception异常求助

问题原因

在.NET 6(以及所有.NET Core版本)中,Process.Start(string filePath)重载的默认行为与.NET Framework不同——它默认将UseShellExecute设置为false。系统双击文件能打开的核心是Windows外壳根据文件扩展名匹配关联程序,而当UseShellExecute=false时,.NET会把传入的文件路径当作可执行程序处理,因此抛出“指定的可执行文件不是适用于此操作系统平台的有效应用程序”的异常。

解决方案

只需显式设置UseShellExecute=true,让系统外壳处理文件的关联打开逻辑即可,提供两种实现方式:

方式一:显式构造ProcessStartInfo

using System.Diagnostics;

var startInfo = new ProcessStartInfo(@"C:\temp\test.eml")
{
    UseShellExecute = true
};
Process.Start(startInfo);

方式二:简化写法(.NET Core 3.0及以上版本可用)

using System.Diagnostics;

Process.Start(new ProcessStartInfo(@"C:\temp\test.txt") { UseShellExecute = true });

补充说明:若项目基于.NET Framework开发,UseShellExecute默认值为true,直接调用Process.Start(filePath)即可正常打开关联文件;但.NET Core及后续版本为了跨平台兼容性,将该属性默认值改为false,因此需要手动设置。

内容的提问来源于stack exchange,提问作者Max Headroom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:01:20