Resolved Launching a program from Net 7 for a given file.

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I have upgraded my project from Net Framework to Net 7.
In Net Framework, when I wanted to open up an application (such as Photo Viewer or Media Player), all I had to do was include the following code :
C#:
Process.Start(<full file path name>)
It would then start the appropriate default program based on the file type.

In Net 7, this does not seem to work. All the examples I have seen, seem to need to specify the actual application to start, e.g.
C#:
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "C:\Windows\notepad.exe";
process.StartInfo.Arguments = @<file name>;
process.Start();

Surely there must be some equivalent code in Net 7 to just open the default application given just the full file path?
 
Solution
Those examples are by people who don't read the documentation properly. The problem here is the ProcessStartInfo.UseShellExecute property. In order to be able to specify a data file path and have it opened in its default application, that property needs to be set to true. It is true by default in .NET Framework apps but it is false by default in .NET Core apps (.NET 5 and later are based on .NET Core). The overload of Process.Start you were using originally creates a ProcessStartInfo internally. If you use your original code in .NET Core then you get an error message that includes this:
An error occurred trying to start process 'file path' with working directory 'project working directory'. The...
Those examples are by people who don't read the documentation properly. The problem here is the ProcessStartInfo.UseShellExecute property. In order to be able to specify a data file path and have it opened in its default application, that property needs to be set to true. It is true by default in .NET Framework apps but it is false by default in .NET Core apps (.NET 5 and later are based on .NET Core). The overload of Process.Start you were using originally creates a ProcessStartInfo internally. If you use your original code in .NET Core then you get an error message that includes this:
An error occurred trying to start process 'file path' with working directory 'project working directory'. The specified executable is not a valid application for this OS platform.
Some people read this and think that they need to specify the path of the executable to open the data file, but they don't. What they need to do is create a ProcessStartInfo themselves and set UseShellExecute to true explicitly:
C#:
var psi = new ProcessStartInfo(dataFilePath) { UseShellExecute = true };

Process.Start(psi);
Using your more verbose code style, that would be this:
C#:
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = dataFilePath;
process.Start();
You don't need to specify the executable because, if UseShellExecute is true, the system will work that out, just as it did in .NET Framework. The only real difference here is the default value of a property and that difference is noted explicitly in the documentation.
 
Solution
As you likely won't use the ProcessStartInfo anywhere else, you can make it even more succinct if you want:
C#:
Process.Start(new ProcessStartInfo(dataFilePath) { UseShellExecute = true });
 
Marvelous, that worked. I've used the more succinct version you provided.

Forum etiquette: please don't quote an entire, massive post, just so you can write "thanks" under it, especially when the post you quoted is the one immediately above yours

Next time just write "thanks" and hit [post reply] (or perhaps just use a combination of Like, Upvote, Mark as Solution etc to convey appreciation)
 
Forum etiquette: please don't quote an entire, massive post, just so you can write "thanks" under it, especially when the post you quoted is the one immediately above yours

Next time just write "thanks" and hit [post reply] (or perhaps just use a combination of Like, Upvote, Mark as Solution etc to convey appreciation)

Ok, will do. Although I noticed that just hitting reply still added the text above.
 
1) you are free to delete any text inserted by the forum when you hit Reply. This way you can snip out just a part of a post you want to respond to. You can also select highlight some of (my) words and click the little reply widget to quote just those words..

2) ..but mainly to make a reply to a thread that does not quote a post, just scroll the page down a bit further and type something in the empty reply box above the Post Reply button. If the box isn't empty, it's because clicking some Reply or Quote button prepopulated it with a quote, in which case you can erase the text
 
Back
Top Bottom