我正在尝试运行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
发布于 2015-05-29 16:48:07
好吧,所以我找到了解决办法。
基本上,正如我们在chat中发现的那样,System32文件夹在某些构建中重定向到SysWOW64,导致quser出现在而不是中。
我成功地应用了一种解决办法。
要解决这个问题:
quser调用之前,进行以下调用:
IntPtr val = IntPtr.Zero;Wow64DisableWow64FsRedirection(参val);quser调用后,还原更改:
Wow64EnableWow64FsRedirection(参考文献);完整样本:
using System.Runtime.InteropServices;
...
namespace CSharpTests
{
public class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int Wow64EnableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int Wow64RevertWow64FsRedirection(ref IntPtr ptr);
static void Main(string[] args)
{
IntPtr val = IntPtr.Zero;
Wow64DisableWow64FsRedirection(ref val);
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c quser";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Wow64RevertWow64FsRedirection(ref val);
p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c quser";
p.Start();
output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
}
}
}结果:
用户名SESSIONNAME ID状态空闲时间登录时间
ebrown console 1 Active none 05/18/2015 09:2
1
'quser' is not recognized as an internal or external command,
operable program or batch file.如您所见,第一个quser调用成功了,因为我们告诉操作系统停止重定向到SysWOW64,一旦我们重新启用它,调用就失败了。
我相信这种保护是有原因的,但有时你不需要它。
其他考虑因素:
如果有人实现此模式,首先检测是否需要应用解决方案,这将是谨慎的做法。这样的检测可以使用以下布尔值来完成:
Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).Contains("System32")如果是假布尔值,则需要检查:
File.Exists(@"c:\windows\System32\FILENAMEHERE")在这种情况下:
File.Exists(@"c:\windows\System32\qdisk.exe")改编自:
http://blog.airesoft.co.uk/2010/09/wow-disabling-wow64-fs-redirection-can-cause-problems-who-knew/
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx
https://stackoverflow.com/questions/30533924
复制相似问题