0

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
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
John Andrews
  • 1
  • 1
  • 3
  • 1
    Use procmon from sysinternals to watch the i/o requests to the file system. http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx – Tony Lee Nov 19 '10 at 04:04
  • Thanks for the info!! Excellent bit of advice. It was checking C:\windows\sysWOW64 – John Andrews Nov 19 '10 at 04:31

3 Answers3

0

Try this one out

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

Community
  • 1
  • 1
Raymund
  • 7,684
  • 5
  • 45
  • 78
  • Just tried it and it didn't help. Thank you for responding though. I will keep this in mind. – John Andrews Nov 19 '10 at 04:00
  • Sorry Raymund. I meant to post this reply on another answer. I believe this is part of it. I just tested as Tony Lee suggested and it's trying to read C:\windows\SysWOW64\ even though if i have it print the path it still shows c:\windows\system32 – John Andrews Nov 19 '10 at 04:30
  • 1
    `Environment.ExpandEnvironmentVariables(@"%systemroot%\Sysnative")` fixed the issue. I found it due to your link!!! – John Andrews Nov 19 '10 at 04:42
0

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            

}
user
  • 16,429
  • 28
  • 80
  • 97
  • I just tested the code and it still returns "File doesn't exist". Any ideas on how to check for the permissions. I'm willing to test anything out. – John Andrews Nov 19 '10 at 04:00
  • Are you 100% sure the file exists within your System32 directory? Are you sure you haven't already deleted the file beforehand? – user Nov 19 '10 at 04:03
  • I can view the file in Windows Explorer and see it's under C:\windows\system32. I've tested with cmd using "del c:\windows\system32\1.txt" and it will delete the file. – John Andrews Nov 19 '10 at 04:09
0

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 ?

Michael Low
  • 24,276
  • 16
  • 82
  • 119