[英]How to specify the Directory structure of Release / Debug Folder after building it
我有一個 c# WPF 應用程序。 在構建我的項目后,我想有一個特定的文件夾,並且所有這些文件夾都包含在 Debug/Release 文件夾中的文件/子目錄。 現在在我的工作站上,我有以下結構/MyProject/Resources/NeededFolder
。
我想在 Build 目錄中有這個完全相同的文件夾,就像這樣/MyProject/bin/Release/NeededFolder
。 我需要/NeededFolder
結構及其所有子目錄和文件才能使我的應用程序正常工作。
處理這個問題的最佳解決方案/最佳實踐方法是什么?
根據您的描述,您希望文件夾及其所有子目錄中的文件結構相同。
您可以將 Copy Directory 方法放在代碼中並定義兩個字符串變量。 一個是源地址,另一個是目標地址。
該方法會將源地址文件夾中的所有文件復制到目標地址的詢價文件夾中。
因此,您可以擁有相同的文件結構和文件夾中的所有文件。
這是一個您可以參考的代碼示例:
private void Button_Click(object sender, RoutedEventArgs e)
{
string sourcePath = @"C:\Users\source\repos\MyProject";
string destPath = @"C:\Users\Desktop\New folder";
CopyDirectory(sourcePath,destPath);
}
public static void CopyDirectory(string srcPath, string destPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo)
{
if (!Directory.Exists(destPath + "\\" + i.Name))
{
Directory.CreateDirectory(destPath + "\\" + i.Name);
}
CopyDirectory(i.FullName, destPath + "\\" + i.Name);
}
else
{
File.Copy(i.FullName, destPath + "\\" + i.Name, true);
}
}
}
catch (Exception e)
{
throw;
}
}
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.