开发者

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

开发者 https://www.devze.com 2023-01-26 08:43 出处:网络
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.t

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
       开发者_C百科     } //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. 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            

}


Try this one out

check if file exist on 64 bits system using File.Exists


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消