【发布时间】:2015-08-12 13:50:20
【问题描述】:
我正在尝试运行 quser 并获取结果字符串。当我运行下面的代码时,我看到以下消息,结果字符串为空:
'quser' 不是内部或外部命令、可运行程序或批处理文件。
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c quser /server:SomeServer";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
所以运行的完整命令是
cmd.exe /c quser /ser:SomeServer
当我直接执行时运行良好,但从 C# 失败。
我在这里找到了一个没有答案的类似问题:Executing Quser windows command in C#; Returning result to String。不过,这个问题没有quser not recognized 消息。
为什么从代码运行时无法识别命令?
我尝试像这样直接运行quser 命令,但我得到一个文件未找到...奇怪
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"c:\Windows\System32\quser.exe";
p.StartInfo.Arguments = @"/server:SomeServer";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
我们发现以 64 位运行它,它会找到它。当以 AnyCPU 或 32 位运行时,它似乎在 SysWOW64 中查找,即使我直接告诉它在 System32 中查找
【问题讨论】:
-
它在谁下运行?这可能是
PATH环境变量问题。 -
您是否尝试过前面带有
@的参数字符串,就像在您的链接问题中一样? -
@MattJones 添加 @ 没有区别
-
@Dleh 另外,你确定这不是魔兽世界64相关的问题吗?我的意思是 x64 Windows 上 x86 程序的路径重定向。
-
@EugenePodskal 我在这里工作的 64 位 Windows 8.1 PC 上运行了相同的代码。
标签: c#