简体   繁体   English

* .jpg-文件的Process.Start()失败

[英]Process.Start() fails for *.jpg-Files

I use the following code to open jpg -files: 我使用以下代码打开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
}

Now, when I do this, I get always an Win32Exception with NativeErrorCode = ERR_NO_ASSOCIATION 现在,当我这样做时,我总是得到一个Win32Exception,其NativeErrorCode = ERR_NO_ASSOCIATION

But the extension *.jpg is associated to MSPaint. 但是扩展名*.jpg与MSPaint关联。

When I double click the File then the File is opened in MSPaint. 当我双击“文件”时,将在MSPaint中打开文件。

Why is there a Win32Exception even the file is associated? 为什么即使文件已关联,也会出现Win32Exception

It's possible that the JPG extension doesn't have an explicit open verb registered for it, in which case you can omit it and the underlying ShellExecute function will use the first registered verb (which, in your case, may be edit ): JPG扩展名可能没有为其注册一个显式的开放动词,在这种情况下,您可以省略它,而底层的ShellExecute函数将使用第一个注册的动词(在您的情况下,可能是edit ):

    var processStartInfo = new ProcessStartInfo { FileName = file };

The ProcessStartInfo.Verbs property contains all the registered verbs for the given file name. ProcessStartInfo.Verbs属性包含给定文件名的所有已注册动词。

Try this : 尝试这个 :

        string file = @"C:\Users\administrator.ADSALL4SPS\Desktop\IMG_4121.JPG";
        System.Diagnostics.Process.Start(file);

Because you want to open this file with default windows application you need to use System.Diagnostics.Process.Start to open files with default applications. 因为要使用默认的Windows应用程序打开此文件,所以需要使用System.Diagnostics.Process.Start打开默认应用程序的文件。

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

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