我正在尝试建立一个自动备份和恢复系统。
我的日志文件:
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# 编程的反馈,因为 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);
}
}
}