使用 C# 从 Windows\System32 目录中删除文件
我正在开发一个 C# 应用程序,需要删除 System32 中的几个文件,我正在执行以下操作:
File.Delete(@"c:\windows\system32\<file>");
这不起作用,它不会引发异常,但也不会删除该文件。我认为这与权限有关,但我不知道如何解决它。你能帮忙吗?
I'm developing a C# application that will need to delete a couple of files in System32, and I'm doing the following:
File.Delete(@"c:\windows\system32\<file>");
This isn't working, it doesn't throw an exception but it also doesn't delete the file. I'm thinking it's related to the permissions, but I'm not sure how to fix it. Can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我们假设你没有做任何恶意的事情;)
无论如何,还没有尝试过,但模拟会有所帮助。
谷歌模拟c#,你会看到很多例子,邮件的想法很简单:你的代码通常在你的用户的权限下运行。通过模拟,您可以在另一个用户的权限下运行您的代码(以编程方式,用户不需要执行任何操作)。因此,如果用户可以直接访问该文件夹而无需 UAC 限制,那么从理论上讲,它应该立即运行。但同样,我还没有尝试过,所以如果它不起作用,请不要生气。只是一个想法。
Well, let's just assume you are not doing something malicious ;)
Anyway, haven't tried it, but Impersonation would help.
Google impersonation c# and you'll see lots of examples, and the mail idea is simple: your code is normally running under the priviledges of your user. By impersonation, you may run your code (programatically, user doesn't need to do anything) under the priviledges of another user. So if a user has direct access to that folder without the UAC restirction, then, theoretically, it should just run then. But again, I haven't tried it, so don't get mad if it doesn't work. Just an idea.
如果您在 Vista 或 7(或 Server 2008+)上执行此操作,UAC 也会妨碍您的删除。在这种情况下,您需要修改应用程序的清单,以便在启动时提升其权限(或启动提升的子应用程序或进程):
http://victorhurdugaci.com/using-uac-with-c-part-1/
此外,如果您发布了你的异常获取,因为这会表明它是与权限相关、与 x64 相关还是与 UAC 相关。
If you're doing this on Vista or 7 (or Server 2008+), UAC will get in the way of your delete as well. In that case, you'll need to modify the manifest of your application so that it elevates its permissions when it starts (or launch an elevated sub-app or process):
http://victorhurdugaci.com/using-uac-with-c-part-1/
Also, it would be helpful if you posted the exception you're getting, since that would suggest whether it's permissions-related, x64-related, or UAC.
您需要管理访问权限才能修改该文件夹中的文件。在属性中使用
app.manifest
文件,如下所示:You need administrative access to modify files in that folder. Use an
app.manifest
file in properties, like so: