简体   繁体   English

使用C#从c:\\ windows \\ system32删除文件时遇到问题

[英]Trouble deleting a file from c:\windows\system32 using C#

Not quite sure why I can't get this file to delete. 不太确定为什么我无法删除该文件。 I'm logged in as Admin, tried "Run as Admin", tried running in the same folder, tried setting permissions on the file, tried creating a test 1.txt file to delete and no luck. 我以Admin身份登录,尝试“以Admin身份运行”,尝试在同一文件夹中运行,尝试在文件上设置权限,尝试创建要删除的测试1.txt文件,但没有运气。 It is acting like the file isn't there. 它的行为就像文件不存在。 I can see it in Windows Explorer. 我可以在Windows资源管理器中看到它。 Please any help is welcome. 欢迎任何帮助。 Thank you for your time. 感谢您的时间。

public void deleteFile(string FileToDelete)
        {            
            //sets system32 to system32 path
            string system32 = Environment.SystemDirectory + @"\";

            //File.SetAttributes(@system32 + FileToDelete, FileAttributes.Normal);

            try
            {
                //check if file exists
                if (!File.Exists(@system32 + @FileToDelete))
                {
                    //if it doesn't no need to delete it
                    Console.WriteLine("File doesn't exist or is has already been deleted.");
                    //Console.WriteLine(system32 + FileToDelete);

                } //end if
                //if it does, then delete
                else
                {
                    File.Delete(system32 + FileToDelete);
                    Console.WriteLine(FileToDelete + " has been deleted.");

                } //end else
            } //end try
            //catch any exceptions
            catch (Exception ex)
            {
                Console.WriteLine(Convert.ToString(ex));
            } //end catch            
        } //end DeleteFile

I created a test file "test.txt" and it worked no problem. 我创建了一个测试文件“ test.txt”,它没有问题。 I should not that I didn't use the method you posted, but rather used the contents of your supplied method and used them within the main() method of a console application. 我不应该不使用您发布的方法,而是使用您提供的方法的内容,并在控制台应用程序的main()方法中使用它们。

ou should also add ReadLine() to display any messages that are returned. 您还应该添加ReadLine()以显示所有返回的消息。

This is what I used, not that it's much different from what you supplied. 这是我使用的,并不是与您提供的内容有很大不同。 If this code doesn't work for you then it must be a system privileged issue. 如果此代码对您不起作用,则必须是系统特权问题。

static void Main(string[] args)
{
    string FileToDelete = "test.txt";
    //sets system32 to system32 path
    string system32 = Environment.SystemDirectory + @"\";

    try
    {
        //check if file exists
        if (!File.Exists(system32 + FileToDelete))
        {
            //if it doesn't no need to delete it
            Console.WriteLine("File doesn't exist or is has already been deleted.");
            //Console.WriteLine(system32 + FileToDelete);
            Console.ReadLine();

        } //end if
        //if it does, then delete
        else
        {
            File.Delete(system32 + FileToDelete);
            Console.WriteLine(FileToDelete + " has been deleted.");
            Console.ReadLine();

        } //end else
    } //end try
    //catch any exceptions
    catch (Exception ex)
    {
        Console.WriteLine(Convert.ToString(ex));
        Console.ReadLine();
    } //end catch            

}

If you're using Vista / Windows 7, maybe you're running into file virtualization issues. 如果您使用的是Vista / Windows 7,则可能会遇到文件虚拟化问题。 Have you tried adding a manifest with a <requestedExecutionLevel level="requireAdministrator"/> line in it ? 您是否尝试过添加带有<requestedExecutionLevel level="requireAdministrator"/>行的清单?

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

相关问题 c#删除System32文件夹中的文件 - c# Deleting a file in System32 folder 使用32位应用程序C#的Sysnative和%windir%检查System.32中的File.Exists - Check File.Exists in System32 with Sysnative and %windir% from 32-bit app C# 如何通过C#语言删除Windows 7 dll文件(在System32文件夹中) - How Delete A Windows 7 dll File (In System32 Folder) By C# Language C#替换System32未经授权的访问中的文件 - C# Replace File in System32 Unauthorized Access C# 应用程序搜索 System32 而不是本地文件 - C# application searches System32 instead of local file C#错误Windows启动时,对路径&#39;C:\\ Windows \\ system32 \\ Com \\ dmp&#39;的访问被拒绝 - C# Error Access to the path 'C:\Windows\system32\Com\dmp' is denied When Windows Startup 如何使用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 c#winforms GetCurrentDirectory返回C:\\ WINDOWS \\ System32 \\设计错误 - c# winforms GetCurrentDirectory returns C:\WINDOWS\System32\ design error 以32位运行时在System32中找不到C#文件 - C# file not found in System32 when running in 32 bit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM