The system cannot find the file specified Exception in Process Start (tscon.exe)

后端 未结 2 1218
难免孤独
难免孤独 2021-01-19 05:12

I am getting \"The system cannot find the file specified Exception\" in Process.Start on tscon

Working:

Process.Start(new ProcessStartInfo(@\"c:\\Win         


        
2条回答
  •  醉话见心
    2021-01-19 05:27

    While it looks like you found a workaround long ago, I have an explanation as to why the issue occurs and an arguably better solution. I ran into the same issue with shadow.exe.

    If you watch with Process Monitor, you'll see that it's actually looking for the file in C:\Windows\SysWOW64\ instead of C:\Windows\system32\ due to File System Redirection and your program being a 32-bit process.

    The workaround is to compile for x64 instead of Any CPU or use P/Invoke to temporarily suspect and re-enable File System Redirection with the Wow64DisableWow64FsRedirection and Wow64RevertWow64FsRedirection Win API functions.

    internal static class NativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
    }
    
    ////////////////
    
    IntPtr wow64backup = IntPtr.Zero;
    if (!Environment.Is64BitProcess && Environment.Is64BitOperatingSystem)
    {                            
        NativeMethods.Wow64DisableWow64FsRedirection(ref wow64backup);
    }
    
    Process.Start(new ProcessStartInfo(@"c:\Windows\System32\tscon.exe", @"0 /dest:console"))
    
    if (!Environment.Is64BitProcess && Environment.Is64BitOperatingSystem)
    {
        NativeMethods.Wow64RevertWow64FsRedirection(wow64backup);
    }
                            }
    

提交回复
热议问题