Reputation: 6289
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?
Upvotes: 2
Views: 1762
Reputation: 100047
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.
Upvotes: 4
Reputation: 2111
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.
Upvotes: 1