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

后端 未结 2 1217
难免孤独
难免孤独 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);
    }
                            }
    
    0 讨论(0)
  • 2021-01-19 05:47

    Oh well, this thing has really got my attention.
    I have finally managed to start the tscon.exe from Process.Start.
    You need to pass your "admin" account info, otherwise you get the 'File not found' error.

    Do in this way

    ProcessStartInfo pi = new ProcessStartInfo();
    pi.WorkingDirectory = @"C:\windows\System32"; //Not really needed
    pi.FileName = "tscon.exe";
    pi.Arguments = "0 /dest:console";
    pi.UserName = "steve";
    System.Security.SecureString s = new System.Security.SecureString();
    s.AppendChar('y');
    s.AppendChar('o');
    s.AppendChar('u');
    s.AppendChar('r');
    s.AppendChar('p');
    s.AppendChar('a');
    s.AppendChar('s');
    s.AppendChar('s');
    pi.Password = s;
    pi.UseShellExecute = false; 
    Process.Start(pi);
    

    also to see the result of the command change the following two lines

    pi.FileName = "cmd.exe";
    pi.Arguments = "/k \"tscon.exe 0 /dest:console\"";
    
    0 讨论(0)
提交回复
热议问题