2

I use the following code to open 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

But the extension *.jpg is associated to MSPaint.

When I double click the File then the File is opened in MSPaint.

Why is there a Win32Exception even the file is associated?

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
BennoDual
  • 5,865
  • 15
  • 67
  • 153

2 Answers2

4

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):

    var processStartInfo = new ProcessStartInfo { FileName = file };

The ProcessStartInfo.Verbs property contains all the registered verbs for the given file name.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
1

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.

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
  • This looks almost the same as what they did in the question, they just used the overload that takes a ProcessStartInfo instead of just the string. Same thing, right? – Moby Disk Feb 02 '15 at 21:25
  • @MobyDisk No. They are different. Read the code again you'll see difference between them. The user tried to use `Verb`, However I didn't use this at all. And this answer is only two lines of codes which can be written even with one line so it's better and gives better compiling time. – Ali Vojdanian Feb 02 '15 at 21:30