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

发布于 2024-10-03 15:57:41 字数 1300 浏览 2 评论 0原文

不太清楚为什么我无法删除该文件。我以管理员身份登录,尝试“以管理员身份运行”,尝试在同一文件夹中运行,尝试设置文件权限,尝试创建要删除的测试 1.txt 文件,但没有成功。它的表现就像该文件不存在一样。我可以在 Windows 资源管理器中看到它。欢迎任何帮助。谢谢您的宝贵时间。

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

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. It is acting like the file isn't there. I can see it in Windows Explorer. 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

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

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

发布评论

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

评论(3

明明#如月 2024-10-10 15:57:41

我创建了一个测试文件“test.txt”,它工作没有问题。我不应该说我没有使用您发布的方法,而是使用您提供的方法的内容并在控制台应用程序的 main() 方法中使用它们。

您还应该添加 ReadLine() 以显示返回的任何消息。

这是我用的,和你提供的没什么不同。如果此代码对您不起作用,那么它一定是系统特权问题。

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            

}

I created a test file "test.txt" and it worked no problem. 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.

ou should also add ReadLine() to display any messages that are returned.

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            

}
月寒剑心 2024-10-10 15:57:41

如果您使用的是 Vista / Windows 7,也许您会遇到 文件虚拟化问题。您是否尝试过添加其中包含 行的清单?

If you're using Vista / Windows 7, maybe you're running into file virtualization issues. Have you tried adding a manifest with a <requestedExecutionLevel level="requireAdministrator"/> line in it ?

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