System.UnauthorizedAccessException: Access to the path C
<p>I am trying to build a automatic backup and restore system.</p>

<p>My logfile:</p>

<pre><code>2012-08-02 22:34:06 - Init: Folder main created for server Teamdeathmatch
2012-08-02 22:34:06 - Backup: Couldn't copy files: System.UnauthorizedAccessException: Access to the path 'C:/temp/backups/Teamdeathmatch/server/main' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at BackupRollback.Backup.getServerFiles() in C:\Users\jordi\Desktop\ConsoleApplication1 - kopie\Backup.cs:line 51
</code></pre>

<p>The target and the file exist.</p>

<p>I hope that anyone can help me. Also some feedback about my programming of c# since c-sharp is new for me. </p>

<p>Thank you</p>

<pre><code>class Backup
{

    public static void getServerFiles()
    {

        Log.Info("Get servers");
        string[] fileEntries = Directory.GetDirectories("c:/Gameservers");

        Log.Info("Servers found: " + fileEntries.Count());

        string _directoryPath = @"C:\temp\backups";

        // check folder exists
        if (!Directory.Exists(_directoryPath))
        {
            Log.Data("Temp backup folder not found");
            Directory.CreateDirectory(_directoryPath);
            Log.Data("Temp backup folder created successfully");

        }
        else
        {
            Log.Data("Temp backup folder found");
        }

        // move files to directory
        foreach (string fileName in fileEntries)
        {
            try
            {
                string servername = Path.GetFileNameWithoutExtension(fileName);

                Directory.CreateDirectory("C:/temp/backups/" + servername + "/server");
                Log.Data("Folder server created for server " + servername);

                Directory.CreateDirectory("C:/temp/backups/" + servername + "/server/main");
                Log.Data("Folder main created for server " + servername);

                File.Copy("C:/temp/backups/" + servername + "/server/main", "C:/Gameservers/" + servername + "/server/main/server.cfg");
                Log.Data("File server.cfg copied for server " + servername);

                Directory.CreateDirectory("C:/temp/backups/" + servername + "/b3 server/conf");
                Log.Data("Folder conf [b3] created for server " + servername);

                File.Copy("C:/temp/backups/" + servername + "/server/main", "C:/Gameservers/" + servername + "/b3 server/conf/b3.xml");
                Log.Data("File b3.xml copied for server " + servername);

                Directory.CreateDirectory("C:/temp/backups/" + servername + "/server/mods");
                Log.Data("Folder mods created for server " + servername);

                DirectoryCopy("C:/temp/backups/" + servername + "/server/", "C:/Gameservers/mods/" + servername + "/server/mods/");
                Log.Data("Folder mods copied for server " + servername);
            }

            catch (Exception ex)
            {
                Log.Data("Couldn't copy files: " + ex.ToString());
            }
        }


    }

    private static void DirectoryCopy(string sourceDirName, string destDirName)
     {
      DirectoryInfo dir = new DirectoryInfo(sourceDirName);
      DirectoryInfo[] dirs = dir.GetDirectories();

        FileInfo[] files = dir.GetFiles();

           foreach (FileInfo file in files)
          {
             string temppath = Path.Combine(destDirName, file.Name);

             file.CopyTo(temppath, false);
         }

         foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);

            DirectoryCopy(subdir.FullName, temppath);
        }
    }
</code></pre>

<p>}</p>
