简体   繁体   English

构建后如何指定发布/调试文件夹的目录结构

[英]How to specify the Directory structure of Release / Debug Folder after building it

I have a c# WPF Application.我有一个 c# WPF 应用程序。 I would like to have a certain folder and all of it's contained files / subdirectorys in the Debug/Release Folder after buildung my Project.在构建我的项目后,我想有一个特定的文件夹,并且所有这些文件夹都包含在 Debug/Release 文件夹中的文件/子目录。 Right now on my working station i have the following structure /MyProject/Resources/NeededFolder .现在在我的工作站上,我有以下结构/MyProject/Resources/NeededFolder

And I would like to have this exact same Folder in the Build directory, like so /MyProject/bin/Release/NeededFolder .我想在 Build 目录中有这个完全相同的文件夹,就像这样/MyProject/bin/Release/NeededFolder I need the /NeededFolder structure and all of its subdirectories and files for my application to work.我需要/NeededFolder结构及其所有子目录和文件才能使我的应用程序正常工作。

What would be the best solution / best practice way to deal with that?处理这个问题的最佳解决方案/最佳实践方法是什么?

Include it/it's children in the project (you may have to click the Show All Files Button to see it) and set any files you want to be output to have a build action of Content or None, depending on how you want them associating with the app (see here and the linked question [there](of Content)) and a Copy to Output of Always or Newer将它/它的子项包含在项目中(您可能必须单击“显示所有文件”按钮才能看到它)并将您想要的任何文件设置为 output 以具有内容或无的构建操作,具体取决于您希望它们如何关联应用程序(请参阅此处和链接的问题 [there](内容))和始终或更新的 Output 的副本

According to your description, you want to have the same file structure in the folder and all of its sub-directories.根据您的描述,您希望文件夹及其所有子目录中的文件结构相同。

You can put the Copy Directory method in your code and define two string variables.您可以将 Copy Directory 方法放在代码中并定义两个字符串变量。 One is the source address and the other is the target address.一个是源地址,另一个是目标地址。

This method will copy all the files in the source address folder to the price inquiry folder of the target address.该方法会将源地址文件夹中的所有文件复制到目标地址的询价文件夹中。

So you can have the same file structure and all files in the folder.因此,您可以拥有相同的文件结构和文件夹中的所有文件。

Here is a code example you can refer to:这是一个您可以参考的代码示例:

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;
            }
        }

Result:结果:

在此处输入图像描述

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

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