0

Im having a bit of a challenge here. Im supposed to backup a computer, and therefore I would like to use c:\ as my origo for a recursive search for files to backup.

using System;
using System.IO;

class Program
{
  static void Main()
  {
    int i;
    string source   = @"c:\";
    string destin   = @"x:\";
    string[] TypeOfFiles = new string[14];
   i=0;
   // Images / Movies:
   TypeOfFiles[i]   = "jpg"; i++;
   TypeOfFiles[i]   = "gif"; i++; 
   TypeOfFiles[i]   = "png"; i++; 
   TypeOfFiles[i]   = "jpeg"; i++;  
   TypeOfFiles[i]   = "tif"; i++;
   TypeOfFiles[i]   = "tiff"; i++;
   TypeOfFiles[i]   = "bmp"; i++; 

   // Adobe
   TypeOfFiles[i]   = "pdf"; i++; 

   // Office Classic:    
   TypeOfFiles[i]   = "doc";  i++;
   TypeOfFiles[i]   = "xls"; i++;
   TypeOfFiles[i]   = "mdb";  i++;

   // Office New:   
   TypeOfFiles[i]   = "docx";  i++;
   TypeOfFiles[i]   = "xlsx";  i++;
   TypeOfFiles[i]   = "mdbx";  i++;

   for (int n = 0; n <= TypeOfFiles.GetUpperBound(0); n++)
   {
     string[] files = Directory.GetFiles("C:\\", "*." + TypeOfFiles[n], SearchOption.AllDirectories);
    foreach (string file in files)
    {
            Console.WriteLine(file);
      }
    }
  }
}

Im running the program from a console (as Administrator). I get this exception: Exception: System.UnauthorizedAccessException: Access to path 'C:\Documents and Settings' was denied. ved System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) ved System.IO.FileSystemEnumerableIterator1.AddSearchableDirsToStack(SearchData localSearchData) ved System.IO.FileSystemEnumerableIterator1.MoveNext() ved System.Collections.Generic.List1..ctor(IEnumerable1 collection) ved System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption) ved Program.Main()

I have also tried: net user administrator /active:yes

I need to access the root. Can anyone help me?

Lars Hansen
  • 155
  • 1
  • 2
  • 16
  • 1
    Well the particular error you are seeing is because `Documents and Settings` is not a "real" folder, it's a [`Junction Point`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb968829(v=vs.85).aspx) used for backwards compatibility to Windows versions previous to Vista/7. You could try to detect and ignore them, but you are going to run into other issues such as with `pagefile.sys` and other operating system files and folders. – Quantic Jul 13 '16 at 22:42
  • 1
    In Windows, there is no root. Instead, in order to bypass security permissions, you have to enable backup privilege. (You may also need to pass special flags depending on which API you're using.) The other problem, as Quantic mentioned, is files that are in use - to get around that, you have to use the volume shadow copy service. – Harry Johnston Jul 13 '16 at 23:37

0 Answers0