greyspace
greyspace

Reputation: 545

'System.UnauthorizedAccessException' but file is still being copied

I have code that I am trying to use to backup the host file. When I run the code, I get the following error:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

The weird thing is that the file is being copied. I am not sure what the code is referring to because if the exception itself wasn't being thrown, everything would be in order (or so it would seem).

The code is as follows:

private void BackUpHost()
        {
            string fileName = "hosts";
            string newFileName = "hosts.bak";
            string sourcePath = @"c:\windows\system32\drivers\etc";
            string targetPath = @"c:\windows\system32\drivers\etc";

            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, newFileName);

            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            System.IO.File.Copy(sourceFile, destFile, true);

            if (System.IO.Directory.Exists(sourcePath))
            {
                    fileName = System.IO.Path.GetFileName(sourcePath);
                    destFile = System.IO.Path.Combine(targetPath, newFileName);
                    System.IO.File.Copy(sourcePath, destFile, true);

            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }
        }

Upvotes: 0

Views: 1115

Answers (1)

Bernd Linde
Bernd Linde

Reputation: 2152

To explain what I meant in my comments, let us "step" through your code and show why you are getting the exception. The below is the same process as putting a breakpoint into your code and using F10 to step over each breakpoint and looking at the "Local Variables" section of the debugger windows.

private void BackUpHost()
{
  string fileName = "hosts";
  string newFileName = "hosts.bak";
  string sourcePath = @"c:\windows\system32\drivers\etc";
  string targetPath = @"c:\windows\system32\drivers\etc";

  string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
  // sourceFile = "c:\windows\system32\drivers\etc\hosts"
  string destFile = System.IO.Path.Combine(targetPath, newFileName);
  // destFile = "c:\windows\system32\drivers\etc\hosts.bak"

  if (!System.IO.Directory.Exists(targetPath))
  {
    System.IO.Directory.CreateDirectory(targetPath);
  }

  System.IO.File.Copy(sourceFile, destFile, true); // First File.Copy() call
  // File "c:\windows\system32\drivers\etc\hosts.bak" is created as a 
  // copy of "c:\windows\system32\drivers\etc\hosts" if either UAC is
  // disabled or the application is run as Administrator.
  // Otherwise "UnauthorizedAccessException - Access to path denied" is thrown

  if (System.IO.Directory.Exists(sourcePath))
  {
    fileName = System.IO.Path.GetFileName(sourcePath); // Setting of fileName is not used in your code again, is this intended or a mistake?
    // fileName = "etc" (Since the value of sourcePath is "c:\windows\system32\drivers\etc", GetFileName() sees the "etc" part as the FileName.
    // Should this not have been GetFileName(sourceFile) ??
    destFile = System.IO.Path.Combine(targetPath, newFileName);
    // destFile = "c:\windows\system32\drivers\etc\hosts.bak"
    System.IO.File.Copy(sourcePath, destFile, true); // Second File.Copy() call
    // This is where your exception happens since you are trying to copy
    // the file "etc" (which is actually a folder)
  }
  else
  {
    Console.WriteLine("Source path does not exist!");
  }
}

The reason why you see a "hosts.bak" file is because of the first File.Copy() call that you do. Since the file is being created, I must assume that you have UAC disabled on your environment or your Visual Studio is being always run as Administrator.

More information about debugging and stepping into/over code can be found here

Upvotes: 3

Related Questions