簡體   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