系统在进程启动 (tscon.exe) 中找不到指定的异常文件

本文关键字:找不到 文件 异常 进程 启动 exe tscon 系统 | 更新日期: 2023-09-27 18:36:29

>我在进程中收到"系统找不到指定的文件异常"。在 tscon 上启动

加工:

Process.Start(new ProcessStartInfo(@"c:'Windows'System32'notepad.exe", "temp.txt"));

不工作:

Process.Start(new ProcessStartInfo(@"c:'Windows'System32'tscon.exe", @"0 /dest:console"));

我需要 tscon.exe。为什么我会收到此错误?

编辑:

  1. 已验证 tscon.exe 是否确实位于c:'Windows'System32文件夹中。
  2. 我在管理员模式下运行VS

该文件是否有一些硬化? 无法理解这一点...

系统在进程启动 (tscon.exe) 中找不到指定的异常文件

哦,好吧,这件事真的引起了我的注意。
我终于设法从Process.Start启动了tscon.exe。
您需要传递"管理员"帐户信息,否则会收到"找不到文件"错误。

以这种方式做

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);

还要查看命令的结果,请更改以下两行

pi.FileName = "cmd.exe";
pi.Arguments = "/k '"tscon.exe 0 /dest:console'"";

虽然看起来您很久以前就找到了解决方法,但我对为什么会出现此问题进行了解释,并且可以说是更好的解决方案。我在影子.exe上遇到了同样的问题。

如果您使用进程监视器观看,您会发现它实际上是在 C:''Windows''SysWOW64'' 而不是 C:''Windows''system32'' 中查找文件,因为文件系统重定向和您的程序是 32 位进程。

解决方法是针对 x64 而不是任何 CPU 进行编译,或者使用 P/Invoke 暂时怀疑并使用 Wow64DisableWow64FsRedirection 和 Wow64RevertWow64FsRedirection Win API 函数重新启用文件系统重定向。

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);
}
                        }