简体   繁体   English

如何通过C#语言删除Windows 7 dll文件(在System32文件夹中)

[英]How Delete A Windows 7 dll File (In System32 Folder) By C# Language

Sometimes I need to delete or replace a dll file in system32 folder of windows 7. 有时我需要删除或替换Windows 7的system32文件夹中的dll文件。
The code below always has Permission Denied Error : 以下代码始终具有Permission Denied Error

            if (File.Exists(@"C:\Windows\system32\mydll.dll"))
            {
                fileInfo.IsReadOnly = false;
                File.Delete(@"C:\Windows\system32\mydll.dll");
            }

How can I bypass this error and replace a file in system32 folder? 如何绕过此错误并替换system32文件夹中的文件?

if (File.Exists(@"C:\Windows\System32\mydll.dll"))
{
    new Process() { StartInfo = new ProcessStartInfo("cmd.exe", @"/k takeown /f C:\Windows\System32\mydll.dll && icacls C:\Windows\System32\mydll.dll /grant %username%:F") }.Start();
    File.Delete(@"C:\Windows\System32\mydll.dll");
}

Note that you can't delete a system DLL like shell32.dll even after taking ownership but you can rename or move it. 请注意,即使拥有所有权也不能删除像shell32.dll这样的系统DLL,但可以重命名或移动它。

A user doesn't have sufficient rights to delete files from c:\\windows\\system32 on Windows Vista and up. 用户没有足够的权限从Windows Vista及更高版本的c:\\ windows \\ system32删除文件。 Even when logged-on using an administrator account. 即使使用管理员帐户登录。 UAC puts a stop to it. UAC停止了它。 You must ask for elevation to let the user know that you are about to tinker with the private parts. 必须要求提升高度,以使用户知道您将要修改私有部分。 That requires embedding a manifest in your program to trigger the UAC prompt. 这需要在您的程序中嵌入清单以触发UAC提示。 This answer shows you how. 这个答案告诉你如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#调用批处理文件无法将dll文件复制到System32文件夹 - C# calling batch file failed to copy dll files to System32 folder c#删除System32文件夹中的文件 - c# Deleting a file in System32 folder 如何在 C# 中获取 Windows\system32\config\systemprofile\AppData\Local\ 文件夹路径? - How to get Windows\system32\config\systemprofile\AppData\Local\ folder path in C#? 如何在C#中更改System32中文件夹的权限? - How to change permissions of a folder inside System32 in C#? 如何使用c#asp.net获取C:\\ Windows \\ System32 \\ inetsrv \\ config文件夹的访问控制 - How to get access control of C:\Windows\System32\inetsrv\config folder using c# asp.net 通过total-commander在C:\\ Windows \\ system32中打开c#文件夹 - c# open folder in C:\Windows\system32 via total-commander Winforms软件上system32文件夹上的DLL - DLL on system32 folder on of winforms software C#类库:StreamWriter写入system32文件夹 - C# Class Library: StreamWriter writing to system32 folder 为什么在system32文件夹中找不到C#库? - Why C# library cannot be found in system32 folder? 使用C#从c:\\ windows \\ system32删除文件时遇到问题 - Trouble deleting a file from c:\windows\system32 using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM