在非静态环境中复制目录

本文关键字:复制 环境 静态 | 更新日期: 2023-09-27 18:34:10

有人可以给我一个在非静态环境中复制目录的提示/示例吗?我试图查找它,但没有这样的运气。

    private void copybutton_Click(object sender, EventArgs e)
    {
        string selectedPath = comboBox1.SelectedItem.ToString();
        copydirectories(@"C:'Mavro'MavBridge'" + selectedPath, @"C:'Mavro'MavBridge'" + selectedPath + " - Copy");
    }
    public void copydirectories()
    {
        try
        {
            string selectedPath = comboBox1.SelectedItem.ToString();
            DirectoryInfo source = new DirectoryInfo(@"C:'Mavro'MavBridge'" + selectedPath);
            DirectoryInfo target = new DirectoryInfo(@"C:'Mavro'MavBridge'" + selectedPath);
            if (!source.Exists)
                return;
            if (!target.Exists)
                target.Create();
            DirectoryInfo[] sourceDirectories = source.GetDirectories();    
            for(int j = 0; j < sourceDirectories.Length; ++j)
            {
                copydirectories(sourceDirectories[j].FullName,target.FullName +"''" + sourceDirectories[j].Name);
            }
            datemodified.Refresh();
   }

那必须采取 2 个参数错误

在非静态环境中复制目录

在 http://xneuron.wordpress.com/2007/04/12/copy-directory-and-its-content-to-another-directory-in-c/中使用代码

public static void CopyAll(DirectoryInfo source, DirectoryInfo target) {
    // Check if the target directory exists, if not, create it.
    if (!Directory.Exists(target.FullName)) {
        Directory.CreateDirectory(target.FullName);
    }
    // Copy each file into its new directory.
    foreach (FileInfo fi in source.GetFiles()) {
        Console.WriteLine(@”Copying {0}'{1}”, target.FullName, fi.Name);
        fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }
    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
        DirectoryInfo nextTargetSubDir = 
            target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

让它变得简单,只需调用它

CopyAll(new DirectoryInfo(@"C:'Mavro'MavBridge'" + selectedPath),
        new DirectoryInfo(@"C:'Mavro'MavBridge'" + selectedPath + " - Copy"));

您是否希望创建具有 2 个参数的函数?

public void copydirectories(string sourcePath, string destinationPath)
    {
        try
        {
            DirectoryInfo source = new DirectoryInfo(sourcePath);
            DirectoryInfo target = new DirectoryInfo(destinationPath);