进程启动中无法找到指定文件的异常 (tscon.exe)。

4

在使用tscon的Process.Start时,我遇到了“系统找不到指定的文件异常”。

解决方法:

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 在 system32 目录下吗?你确定你可以使用你的凭据访问那个软件吗? - Marco
尝试使用“以管理员身份运行…”来运行已编译的exe文件,以查看是否存在权限问题。 - ericosg
1
只能确认原帖的说法。我也尝试了以下代码:ProcessStartInfo pi = new ProcessStartInfo(); pi.FileName = "cmd"; pi.WorkingDirectory = @"C:\windows\System32"; pi.Arguments = "@/k \"tscon.exe 0 /dest:console\""; 结果是一样的。但是,如果我手动打开命令提示符,则可以正常运行。 - Steve
再问一遍,只是为了确认,你有检查过 tscon.exe 是否位于 c:\Windows\System32\ 吗?我问这个是因为在我的 Windows 7 x64 Home Edition 中,它并不在那个位置。但我在 C:\Windows\winsxs\amd64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_6.1.7601.17514_none_42d65ed50fa3c682 找到了它。 - Răzvan Flavius Panda
@RăzvanPanda 如果您以管理员权限打开命令提示符(以管理员身份运行),则会看到该文件。 - Steve
@Steve:我尝试了,但还是没有出现。我也启用了文件夹选项中的显示隐藏文件和受保护操作系统文件,但似乎仍然没有出现。我认为可能在家庭版中默认未安装。 - Răzvan Flavius Panda
2个回答

7

噢,这件事真的引起了我的注意。
我终于成功启动了tscon.exe,使用Process.Start方法。
你需要传递你的“管理员”账户信息,否则会出现“文件未找到”的错误。

按照以下方式进行操作:

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\"";

谢谢您的努力,但我遇到了异常“”The stub received bad data"。 我已经验证了用户名和密码是正确的,我尝试过使用USERNAME或DOMAIN\USERNAME。仍然无法工作。 - user829174
然而,我认为这个错误来自于TSCON,可能是另一个问题。请尝试我的第二个示例,不要使用参数"/k \"tscon.exe /?\""; - Steve
“/dest:console” 是什么意思? - Learner

2
虽然看起来您早已找到了解决方法,但我可以解释一下出现问题的原因,并提供一个更好的解决方案。我也遇到过 shadow.exe 的同样问题。
如果您使用 Process Monitor 进行观察,您会发现它实际上在 C:\Windows\SysWOW64\ 而不是 C:\Windows\system32\ 中查找文件,这是由于 文件系统重定向 和您的程序是 32 位进程导致的。
解决方法是编译为 x64,而不是 Any 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);
}
                        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接