使用目录结构进行C#文件备份

时间:2015-02-24 10:06:02

标签: c#

我的位置如下:C:\Backups

我有多个位置,例如:

C:\Users\Peter\Books
C:\ProgramData\Library
C:\Tests\Testing\Tester\

如何将每个位置中的所有内容复制到备份位置,以便如果我将备份位置文件夹中的所有内容都放入C:它会覆盖备份中的所有文件?基本上我如何创建整个备份结构。

到目前为止:

public void Init()
{      
    _locationsToBackup = new List<string>();

    DataSet dataSet = _settings.Pull();// dataset is set here
    DataTable settingsTable = dataSet.Tables[0];


    int valueIndex = 0;
    foreach (DataColumn coll in settingsTable.Columns)
    {
        if (coll.ColumnName == "Value")
        {
            break;
        }
        valueIndex++;
    }


    foreach (DataRow row in settingsTable.Rows)
    {
        int start = 0;
        foreach (DataColumn col in settingsTable.Columns)
        {
            if (col.ColumnName == "SettingType")
            {
                string settingRowValue = row.ItemArray[start].ToString();
                int settingType = Convert.ToInt32(settingRowValue);
                if (settingType == 3)
                {
                    String location = row.ItemArray[valueIndex].ToString();
                    AddNewLocationToBackup(location);
                    break;

                }

            }
            start++;
        }
    }
}

public void StartBackup()
{
    //make sure backup folder exists in correct location
    // if not then create it.
    if(!Directory.Exists(@"C:\ProgramData\Test\Backups"))
    {
        Directory.CreateDirectory(@"C:\ProgramData\Test\Backups");
    }

    foreach(string currentLocation in LocationsToBackup)
    {
         if (Directory.Exists(currentLocation))
            {
                // copy all files from currentLocation and put into backups
                CopyFilesToDirWithSamePath(currentLocation, @"C:\ProgramData\Test\Backups" + @"\" + currentLocation);
            }
        }
    }
}

public void CopyFilesToDirWithSamePath(string sourceDirInput, string targetDirInput)
{
    string sourceDir = sourceDirInput;
    string targetDir = targetDirInput;
    foreach (var file in Directory.GetFiles(sourceDir))
    {
        File.Copy(file, System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file)), true);
    }
}

3 个答案:

答案 0 :(得分:1)

//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", 
    SearchOption.AllDirectories))
    Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", 
    SearchOption.AllDirectories))
    File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);

我希望这会对你有所帮助

答案 1 :(得分:0)

&#13;
&#13;
 private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory doesn't exist, create it. 
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        // If copying subdirectories, copy them and their contents to new location. 
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我使用FileSelectionManager库,看看这个例子: http://www.fileselectionmanager.com/#Copying%20and%20moving%20files

希望它能解决。