I need to let a .reg file and a .msi file execute automatically using whatever executables these two file types associated with on user\'s Windows.
.NET Core 2.0 Pro
use this to open a file
new ProcessStartInfo(@"C:\Temp\1.txt").StartProcess();
need this extension method
public static class UT
{
public static Process StartProcess(this ProcessStartInfo psi, bool useShellExecute = true)
{
psi.UseShellExecute = useShellExecute;
return Process.Start(psi);
}
}
In case this bothers you as well:
For those of us that are used to simply call Process.Start(fileName);
the above syntax may give us anxiety... So may I add that you can write it in a single line of code?
new Process { StartInfo = new ProcessStartInfo(fileName) { UseShellExecute = true } }.Start();
You can also set the UseShellExecute
property of ProcessStartInfo
to true
var p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\Users\user2\Desktop\XXXX.reg")
{
UseShellExecute = true
};
p.Start();
Seems to be a change in .net Core, as documented here.
You have to execute cmd.exe
var proc = Process.Start(@"cmd.exe ",@"/c C:\Users\user2\Desktop\XXXX.reg")
don't forget the /c
You can set UseShellExecute to true and include this and your path in a ProcessStartInfo object:
Process.Start(new ProcessStartInfo(@"C:\Users\user2\Desktop\XXXX.reg") { UseShellExecute = true });