In .NET Core, one of the changes that caught me by surprise is the change related to starting processes. In the .NET framework – you can open a web site, file, etc. just by using the following:\
System.Diagnostics.Process.Start(path);
However, in .NET Core – this won’t work. When trying to open a file, the process will fail – reporting that a program isn’t associated with the file type. When trying to open a folder on the system, the process will fail with a permission error unless the application is running with administrator permissions (which you don’t want to be doing). The change is related to a change in a property default – specifically:
System.Diagnostics.ProcessStartInfo.UseShellExecute
In the .NET framework – this property is set to true by default. In the .NET Core, it is set to false. The difference here probably makes sense – .NET Core is meant to be more portable and you do need to change this value on some systems. To fix this, I’d recommend removing any direct calls to this assembly and run in through a function like this:
<code>
public static void OpenURL(string url) { var psi = new System.Diagnostics.ProcessStartInfo { FileName = url, UseShellExecute = true }; try { System.Diagnostics.Process.Start(psi); } catch { psi.UseShellExecute = false; System.Diagnostics.Process.Start(psi); } } public static void OpenFileOrFolder(string spath, string sarg = "") { var psi = new System.Diagnostics.ProcessStartInfo { FileName = spath, UseShellExecute = true }; try { System.IO.FileAttributes attr = System.IO.File.GetAttributes(spath); if ((attr & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) { System.Diagnostics.Process.Start(psi); } else { if (sargs.Trim().Length !=0) { psi.Arguments = sargs; } System.Diagnostics.Process.Start(psi); } } catch { psi.UseShellExecute = false; System.IO.FileAttributes attr = System.IO.File.GetAttributes(spath); if ((attr & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) { System.Diagnostics.Process.Start(psi); } else { if (sargs.Trim().Length !=0) { psi.Arguments = sargs; } System.Diagnostics.Process.Start(psi); } }
Since this vexed me for a little bit – I’m putting this here so I don’t forget.
tr