c# - 程序 (c#) 运行提升,仍然无法访问某些文件

标签 c# windows uac

我的代码正在提升运行,但无法看到/执行任何内容 c:\windows\system32\rstrui.exe(系统还原点 UI)。

我仔细检查以确保代码确实在提升运行(确实如此),并且我已经使用 File.Exist() 和 Directory.GetFiles() 以及 System.Diagnostics.Process.Start() 进行了测试;返回的是未找到的文件。

程序真的在那里,我真的可以运行它,我可以复制/粘贴路径到 DOS 并列出它,执行它(无隐藏空格等)。

有什么想法吗?

解决方案:

Tim(下)给了我解决方案,而不是引用 c:\windows\system32 引用 c:\windows\sysnative。 “sysnative”被重定向,或者,好吧,没有。我对 x64 重定向的东西仍然有点困惑。关键是,以下工作:

Environment.GetEnvironmentVariable("windir") + @"\sysnative"

谢谢蒂姆!

最佳答案

让我猜猜...您在 64 位 Windows 安装上作为 x86 应用程序运行? 我不确定原因,但是 this thread提供了一些信息。 如果您为 native 64 位(或我想的任何 CPU)而不是 x86 编译它,它将正确找到文件。

这是因为在 WOW64 下运行的应用程序的 64 位操作系统中正在进行系统文件夹重定向。您可以阅读更多相关信息 here .该帖子的一个特别相关的部分是谈论如何绕过它:

Applications can control the WOW64 file system redirector using the Wow64DisableWow64FsRedirection, Wow64EnableWow64FsRedirection, and Wow64RevertWow64FsRedirection functions. Disabling file system redirection affects all file operations performed by the calling thread, so it should be disabled only when necessary for a single CreateFile call and re-enabled again immediately after the function returns. Disabling file system redirection for longer periods can prevent 32-bit applications from loading system DLLs, causing the applications to fail.

32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32. WOW64 recognizes Sysnative as a special alias used to indicate that the file system should not redirect the access. This mechanism is flexible and easy to use, therefore, it is the recommended mechanism to bypass file system redirection. Note that 64-bit applications cannot use the Sysnative alias as it is a virtual directory not a real one.

我无法让 %windir%\Sysnative 想法发挥作用,但我希望这对你有用,或者你可以实现重定向的启用/禁用。

关于c# - 程序 (c#) 运行提升,仍然无法访问某些文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6035910/

相关文章:

c# - Tesseract OCR 输出错误

c# - 将 C++(处理 Corona)函数转换为 C#

php 窗口在 windows 中比 linux 慢 4~5 倍

c++ - 适用于 Windows 的 Memcached 客户端库

c - 从命名管道获取客户端用户 token

c# - 开发以管理员身份运行命令的应用程序

c# - 使用 SimpleInjector 的同一组单例进行多个 RegisterAll 注册

c# - 黑莓中的加密和.net webservices中的解密

c++ - 使用 WinApi 获取与 "Advanced display settings"面板中相同的显示名称

c# - 确定对应用程序文件夹的写入权限