OriginalGriff
По странному совпадению, я только что сбил приложение резервного копирования / восстановления сегодня утром (после того, как поднял свои DOOM savegames на один и тот же уровень в третий раз за два дня...)
/// <summary>
/// Backup save games
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butBackup_Click(object sender, EventArgs e)
{
if (EnsureFoldersExist(saveGamePath, backupSaveGamePath))
{
CopyFolder(saveGamePath, backupSaveGamePath);
}
}
/// <summary>
/// Restore save games
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butRestore_Click(object sender, EventArgs e)
{
if (EnsureFoldersExist(backupSaveGamePath, saveGamePath))
{
CopyFolder(saveGamePath, backupSaveGamePath);
}
}
/// <summary>
/// Copy a folder from one place to another.
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
private void CopyFolder(string source, string destination)
{
try
{
DirectoryInfo dir = new DirectoryInfo(source);
if (!dir.Exists) throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + source);
if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);
// Copy files
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destination, file.Name);
file.CopyTo(temppath, true);
}
//Copy folders
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destination, subdir.Name);
CopyFolder(subdir.FullName, temppath);
}
}
catch (Exception e)
{
MessageBox.Show("Unable to copy data.\nThe error reported is\n \"" + e.Message + "\"", "Problem with required folders", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Ensure input and output folders exist.
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
/// <returns>True if folders exist OK</returns>
private bool EnsureFoldersExist(string source, string destination)
{
if (!Directory.Exists(source))
{
MessageBox.Show("Input path does not exist:\n " + source, "Problem with required folders", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (!Directory.Exists(destination))
{
// Create it.
try
{
Directory.CreateDirectory(destination);
}
catch (Exception e)
{
MessageBox.Show("Unable to create output path:\n " + destination + "\nThe error reported is\n \"" + e.Message + "\"", "Problem with required folders", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
return true;
}
Все, что вам нужно сделать, это вырвать часть копии файла.