.Net Core 2.0 Process.Start throws “The specified executable is not a valid application for this OS platform”

前端 未结 5 514
走了就别回头了
走了就别回头了 2020-12-03 16:34

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

相关标签:
5条回答
  • 2020-12-03 17:11

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:17

    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();
    
    0 讨论(0)
  • 2020-12-03 17:18

    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.

    0 讨论(0)
  • 2020-12-03 17:28

    You have to execute cmd.exe

    var proc = Process.Start(@"cmd.exe ",@"/c C:\Users\user2\Desktop\XXXX.reg")
    

    don't forget the /c

    0 讨论(0)
  • 2020-12-03 17:29

    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 });
    
    0 讨论(0)
提交回复
热议问题