繁体   English   中英

如何遍历目录以覆盖文件?

[英]How to iterate through a directory to overwrite a file?

我有一个在用户配置文件中已经创建的目录路径。 下面的代码创建目录,然后将custom.xml复制到Customize文件夹中。 此代码创建文件夹,而不仅仅是深入到用户配置文件和所有子目录。 我只需要覆盖Customize文件夹,或者只需要将Customize.xml文件添加到其中,并覆盖可能已经存在的那个文件夹。 我该怎么做呢?

string fileName = "Customize.xml"; 
string targetPath = @"\\desktoppath\c$\%USERPROFILE%\%APPDATA%\%Roaming%\%Litera%\Customize";
//string targetPath = @"\\desktoppath\c$\TestFolder";
// Use Path class to manipulate file and directory paths.
string sourceFile = Path.Combine(sourcePath, fileName);
string destFile = Path.Combine(targetPath, fileName);

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

这仅仅是一件事:

string fileNamenameToCopy = "D:\\Folder1\\Customize.xml"; 
File.Copy(fileNamenameToCopy, targetPath+ "\\Customize.xml");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM