簡體   English   中英

構建后如何指定發布/調試文件夾的目錄結構

[英]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結構及其所有子目錄和文件才能使我的應用程序正常工作。

處理這個問題的最佳解決方案/最佳實踐方法是什么?

將它/它的子項包含在項目中(您可能必須單擊“顯示所有文件”按鈕才能看到它)並將您想要的任何文件設置為 output 以具有內容或無的構建操作,具體取決於您希望它們如何關聯應用程序(請參閱此處和鏈接的問題 [there](內容))和始終或更新的 Output 的副本

根據您的描述,您希望文件夾及其所有子目錄中的文件結構相同。

您可以將 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM