【问题标题】:The system cannot find the file specified Exception in Process Start (tscon.exe)系统找不到进程启动中指定的文件异常(tscon.exe)
【发布时间】:2012-06-07 07:31:55
【问题描述】:

我在 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 目录中吗?您确定可以使用您的凭据访问该软件吗?
  • 尝试使用“以管理员身份运行..”运行您编译的 exe,看看它是否没有权限。
  • 只能确认OP语句。也试过ProcessStartInfo pi = new ProcessStartInfo();pi.FileName = "cmd";pi.WorkingDirectory = @"C:\windows\System32";pi.Arguments = "@/k \"tscon.exe 0 /dest:console\"";,结果是一样的。但是,如果我从手动打开的命令提示符中尝试,它可以工作。
  • 再次询问,只是为了确定,您是否检查过tscon.exe 位于c:\Windows\System32` ? I'm asking cause I don't have it at that location in my Windows 7 x64 Home Edition. I did found it in: C:\Windows\winsxs\amd64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_6.1.7601.17514_none_42d65edcfa6825 ` 不过。
  • @RăzvanPanda 如果您以管理员权限打开命令提示符(以管理员身份运行),您将看到该文件。

标签: c# process


【解决方案1】:

哦,好吧,这件事确实引起了我的注意。
我终于设法从 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\"";

【讨论】:

  • 感谢您的努力,但是我收到异常“存根收到错误数据”。我验证了用户名和密码正确,我尝试输入 USERNAME 或 DOMAIN\USERNAME。还是不行
  • 但是我认为这个错误来自 TSCON,也许是另一个问题。试试我的第二个例子,不带参数"/k \"tscon.exe /?\"";
  • /dest:console 是做什么的?
【解决方案2】:

虽然您似乎很久以前就找到了解决方法,但我解释了问题发生的原因以及可以说是更好的解决方案。我在 shadow.exe 中遇到了同样的问题。

如果您使用 Process Monitor 观看,您会发现它实际上是在 C:\Windows\SysWOW64\ 而不是 C:\Windows\system32\ 中寻找文件,因为 File System Redirection 并且您的程序是 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);
}
                        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2014-03-30
    相关资源
    最近更新 更多