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

发布于 2024-11-08 05:07:09 字数 508 浏览 4 评论 0原文

我的代码正在提升运行,但没有任何东西可以看到/执行 c:\windows\system32\rstrui.exe(系统还原点 UI)。

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

该程序确实存在,我真的可以运行它,我可以将路径复制/粘贴到DOS并列出它,执行它(没有隐藏空格等)。

有什么想法吗?

解决方案:

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

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

谢谢蒂姆!

My code is running elevated but nothing can see/execute c:\windows\system32\rstrui.exe (System Restore Point UI).

I double checked to make sure the code really was running elevated (it is), and I've tested with File.Exist() and Directory.GetFiles() and System.Diagnostics.Process.Start(); file not found is what is returned.

The program is really there, I can really run it, I can copy/paste the path to DOS and list it, execute it (no hidden spaces, etc.).

Any ideas?

THE SOLUTION:

Tim (below) gave me the solution, instead of referencing c:\windows\system32 reference c:\windows\sysnative. The "sysnative" gets redirected, or, well, doesn't. I'm still a bit confused about x64 redirection stuff. The point is, the following works:

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

Thanks Tim!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-11-15 05:07:09

让我猜猜...您在 64 位 Windows 安装上作为 x86 应用程序运行吗?
我不确定原因,但是 此线程提供了一些信息。
如果您针对本机 64 位(或我认为的任何 CPU)而不是 x86 编译它,它会正确找到该文件。

其原因是 64 位操作系统中在 WOW64 下运行的应用程序上发生的系统文件夹重定向。您可以在此处了解更多相关信息。该帖子中有一个特别相关的部分是讨论如何绕过它:

应用程序可以控制WOW64
文件系统重定向器使用
Wow64DisableWow64FsRedirection,
Wow64EnableWow64FsRedirection,以及
Wow64RevertWow64FsRedirection
功能。禁用文件系统
重定向影响所有文件
调用执行的操作
线程,所以应该只禁用它
当需要单个 CreateFile 时
立即拨打电话并重新启用
函数返回后。禁用
文件系统重定向时间更长
句点可以阻止 32 位
应用程序加载系统DLL,
导致应用程序失败。

32位应用程序可以访问
本机系统目录
替换 %windir%\Sysnative
%windir%\System32。 WOW64承认
Sysnative 作为特殊别名用于
表明文件系统应该
不重定向访问。这
机制灵活且易于使用,
因此,推荐
绕过文件系统的机制
重定向。请注意,64 位
应用程序无法使用 Sysnative
别名,因为它是虚拟目录而不是
一个真实的。

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

Let me guess... you're running as an x86 application on a 64 bit installation of Windows?
I'm not sure of the reason, but this thread provides some information.
If you compile it for native 64 bit (or Any CPU I suppose) instead of x86, it'll find the file properly.

The reason for this is the System Folder Redirection that's going on in 64bit OS's on applications that are running under WOW64. You can read more about it here. One particularly relevant chunk of that post is talking about how to get around it:

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.

I was unable to get the %windir%\Sysnative idea working, but I'm hopeful that either that works for you or you can implement the enabling/disabling of redirection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文