c# - 怎么把a文件夹的文件移到b文件夹

标签 c# copy directory

我想将文件从一个目录复制到另一个目录,但它不起作用。发生的错误

Can't make dir if he is or something like that

这是我的代码

string uplaydir = "";
using (StreamReader sr = new StreamReader("src\\SYSTEM\\launcherfiles\\uplay_dir.txt"))
{
    uplaydir = sr.ReadLine();
}
label2.Text = "Installing";
ExtractZipFile(@"src\\SYSTEM\\launcherfiles\\updatefiles\\vmr.zip", @"src\\SYSTEM\\launcherfiles\\updatetemp");
label2.Text = "Done!";
File.Copy(@"src\\SYSTEM\\launcherfiles\\updatetemp\\ubi", @uplaydir);

uplay_dir.txt 位于 c:\test\

uplay_cm.dll 位于 src\\SYSTEM\\launcherfiles\\updatetemp\\ubi

我该如何修复?

最佳答案

MSDN链接向您展示了如何执行此操作。

按照此代码在目录之间复制文件:

string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
    System.IO.Directory.CreateDirectory(targetPath);
}

// To copy a file to another location and 
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);

// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
//       in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
    string[] files = System.IO.Directory.GetFiles(sourcePath);

    // Copy the files and overwrite destination files if they already exist.
    foreach (string s in files)
    {
        // Use static Path methods to extract only the file name from the path.
        fileName = System.IO.Path.GetFileName(s);
        destFile = System.IO.Path.Combine(targetPath, fileName);
        System.IO.File.Copy(s, destFile, true);
    }
}
else
{
    Console.WriteLine("Source path does not exist!");
}

关于c# - 怎么把a文件夹的文件移到b文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985208/

相关文章:

ftp - 将文本文件复制到 FTP 站点的批处理文件

android - Android Studio 中 'drawable' 和 'drawable-v24' 之间的区别?

linux - 如何在不同的目录中包含相同的文件?

c# - 获取 NullReferenceException 的变量名

PostgreSQL COPY,是否可以一次将数据写入多个表

c++ - 函数调用参数总是一个新对象吗?

c# - 如何从文件名的前几个字母创建文件夹?

c# - 如何从 Facebook 注销或清除 WebAuthenticationBroker 上的 cookie?

c# - 将凭据传递给 Sql Report Server 2008

c# - LINQ 从第一个列表中选择