首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在32位运行时,C#文件在System32中找不到

在32位运行时,C#文件在System32中找不到
EN

Stack Overflow用户
提问于 2015-05-29 15:53:00
回答 1查看 1.5K关注 0票数 3

我正在尝试运行quser并获取结果字符串。当我运行下面的代码时,我看到以下消息,结果的字符串是空的:

“‘quser”不被识别为内部或外部命令、可操作的程序或批处理文件。

代码语言:javascript
复制
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);

因此,运行的完整命令是

代码语言:javascript
复制
cmd.exe /c quser /ser:SomeServer

当我直接执行它时,它运行良好,但是C#失败了。

我在这里发现了一个类似的问题,但没有答案:Executing Quser windows command in C#; Returning result to String。不过,这个问题没有quser not recognized消息。

为什么在从代码中运行时不能识别该命令?

我试着像这样直接运行quser命令,但是没有找到一个文件.奇怪

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-29 16:48:07

好吧,所以我找到了解决办法。

基本上,正如我们在chat中发现的那样,System32文件夹在某些构建中重定向到SysWOW64,导致quser出现在而不是中。

我成功地应用了一种解决办法。

要解决这个问题:

  1. 使用以下命名空间: 使用System.Runtime.InteropServices;
  2. 在类文件的顶部添加以下内容。 DllImport("kernel32.dll",SetLastError =真)公共静态extern int Wow64DisableWow64FsRedirection(ref IntPtr Ptr),DllImport("kernel32.dll",SetLastError = true)公共静态extern Wow64EnableWow64FsRedirection(ref IntPtr ptr);
  3. 在进行quser调用之前,进行以下调用: IntPtr val = IntPtr.Zero;Wow64DisableWow64FsRedirection(参val);
  4. 发出quser调用后,还原更改: Wow64EnableWow64FsRedirection(参考文献);

完整样本:

代码语言:javascript
复制
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状态空闲时间登录时间

代码语言:javascript
复制
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,一旦我们重新启用它,调用就失败了。

我相信这种保护是有原因的,但有时你不需要它。

其他考虑因素:

如果有人实现此模式,首先检测是否需要应用解决方案,这将是谨慎的做法。这样的检测可以使用以下布尔值来完成:

代码语言:javascript
复制
Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).Contains("System32")

如果是假布尔值,则需要检查:

代码语言:javascript
复制
File.Exists(@"c:\windows\System32\FILENAMEHERE")

在这种情况下:

代码语言:javascript
复制
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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30533924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档