简体   繁体   English

使用 System.Diagnostics.Process.Start 打开 EML 文件时出现异常

[英]Exception when using System.Diagnostics.Process.Start to open an EML file

If you save an email to a file on disk with an EML extension, you can open it in Outlook on Windows just by double-clicking the file.如果将电子邮件保存到磁盘上具有 EML 扩展名的文件,则只需双击该文件即可在 Windows 版 Outlook 中将其打开。 I'm hoping to do something similar as my application can encounter EML files that it needs to open.我希望做一些类似的事情,因为我的应用程序可以遇到它需要打开的 EML 文件。

I have some crude test code that has:我有一些粗略的测试代码:

using System;
using System.Diagnostics;
using System.IO;

and then:接着:

System.Diagnostics.Process.Start(@"C:\temp\test.eml");

When I run the code I get the following exception:当我运行代码时,出现以下异常:

System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'C:\temp\test.eml' with working directory 'C:\Users\me\source\repos\TestProj\TestProj\bin\Release\net6.0-windows'. System.ComponentModel.Win32Exception: '尝试使用工作目录 'C:\Users\me\source\repos\TestProj\TestProj\bin\Release\net6.0 启动进程 'C:\temp\test.eml' 时发生错误-视窗'。 The specified executable is not a valid application for this OS platform.'指定的可执行文件不是此操作系统平台的有效应用程序。

I have tried placing a simple text file in the same folder and named it 'test.txt' and checked that it opens in Notepad when I double-click it, but if I try to open it with:我尝试将一个简单的文本文件放在同一文件夹中并将其命名为“test.txt”,并检查当我双击它时它是否在记事本中打开,但是如果我尝试使用以下方式打开它:

System.Diagnostics.Process.Start(@"C:\temp\test.txt");

I get the same error.我犯了同样的错误。 Where am I going wrong?我哪里错了?

There was a breaking change in .NET Core compared to .NET Framework : 与 .NET Framework 相比,.NET Core发生了重大变化:

Change in default value of UseShellExecute更改 UseShellExecute 的默认值
ProcessStartInfo.UseShellExecute has a default value of false on .NET Core. ProcessStartInfo.UseShellExecute在 .NET Core 上的默认值为 false。 On .NET Framework, its default value is true.在 .NET Framework 上,其默认值为 true。

Change description变更说明
Process.Start lets you launch an application directly, for example, with code such as Process.Start("mspaint.exe") that launches Paint. Process.Start允许您直接启动应用程序,例如,使用诸如启动 Paint 的Process.Start("mspaint.exe")类的代码。 It also lets you indirectly launch an associated application if ProcessStartInfo.UseShellExecute is set to true.如果ProcessStartInfo.UseShellExecute设置为 true,它还允许您间接启动关联的应用程序。 On .NET Framework, the default value for ProcessStartInfo.UseShellExecute is true, meaning that code such as Process.Start("mytextfile.txt") would launch Notepad, if you've associated.txt files with that editor.在 .NET Framework 上, ProcessStartInfo.UseShellExecute的默认值为 true,这意味着如果您已将 .txt 文件与该编辑器相关联,则Process.Start("mytextfile.txt")等代码将启动记事本。 To prevent indirectly launching an app on .NET Framework, you must explicitly set ProcessStartInfo.UseShellExecute to false.要防止在 .NET Framework 上间接启动应用程序,您必须将ProcessStartInfo.UseShellExecute显式设置为 false。 On .NET Core, the default value for ProcessStartInfo.UseShellExecute is false.在 .NET Core 上, ProcessStartInfo.UseShellExecute的默认值为 false。 This means that, by default, associated applications are not launched when you call Process.Start .这意味着,默认情况下,当您调用Process.Start时,关联的应用程序不会启动。

Use Process.Start overload accepting ProcessStartInfo and set UseShellExecute to true :使用Process.Start重载接受ProcessStartInfo并将UseShellExecute设置为true

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

Otherwise provide path to executable and pass path to file as a parameter:否则提供可执行文件的路径并将文件路径作为参数传递:

var pathToOutlook = @"C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE";
Process.Start(pathToOutlook,  @"D:\Downloads\sample.eml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM