我使用以下代码打开jpg -files:

var file = @"C:\Users\administrator.ADSALL4SPS\Desktop\IMG_4121.JPG";
var processStartInfo = new ProcessStartInfo { Verb = "open", FileName = file };
var workDir = Path.GetDirectoryName(file);
if (!string.IsNullOrEmpty(workDir)) {
    processStartInfo.WorkingDirectory = workDir;
}
try {
    Process.Start(processStartInfo);
} catch (Exception e) {
    // Errorhandling
}

现在,当我这样做时,我总是得到带有NativeErrorCode = ERR_NO_ASSOCIATION的Win32Exception

但是扩展名*.jpg与MSPaint相关联。

当我双击"file"时,将在MSPaint中打开文件。

为什么即使文件已关联,也存在Win32Exception

最佳答案

JPG扩展名可能没有为其注册一个明确的开放动词,在这种情况下,您可以忽略它,并且底层 ShellExecute 函数将使用第一个注册的动词(在您的情况下,可能是 edit ):

    var processStartInfo = new ProcessStartInfo { FileName = file };

ProcessStartInfo.Verbs 属性包含给定文件名的所有已注册动词。

关于c# - * .jpg-文件的Process.Start()失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28286034/

10-17 00:07