System.UnauthorizedAccessException:访问路径C.

时间:2012-08-02 21:03:40

标签: c#

我正在尝试构建一个自动备份和恢复系统。

我的日志文件:

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

目标和文件存在。

我希望有人能帮助我。另外一些关于我对c-sharp编程的反馈对我来说是新的。

谢谢

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);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

File.Copy(来源,目的地)

您的参数已被颠倒。试试这个。

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