0

I have a c# WPF Application. 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. Right now on my working station i have the following structure /MyProject/Resources/NeededFolder.

And I would like to have this exact same Folder in the Build directory, like so /MyProject/bin/Release/NeededFolder. I need the /NeededFolder structure and all of its subdirectories and files for my application to work.

What would be the best solution / best practice way to deal with that?

Arne_22
  • 57
  • 7

2 Answers2

0

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

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Does this produce the exact same folder structure in my output directory? Because i have about 40 FIles in this Folder i would need to click manually that way. – Arne_22 Jul 31 '20 at 07:29
  • You can select multiple files (click, hold shift, click). You only have to do this once – Caius Jard Jul 31 '20 at 08:05
  • Thanks alot! I tried it for 1 or 2 files and so far it worked for me, thanks alot! – Arne_22 Aug 11 '20 at 05:53
  • Yuo're welcome! If you end up using an answer given on SO, you should click the grey tick mark on the left to turn it green. This lets other people know what worked for you and also marks the question as answered on the dashboard. You can additionally click the up arrow if it, or any other answers, were useful. Any number of answers can be up-arrow'd but only one can be ticked – Caius Jard Aug 11 '20 at 06:36
0

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. 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:

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • That was my first idea too. But since i want this to happen at the same timer / after Visual Studio builds my code. That means I can't run this method directly. – Arne_22 Aug 11 '20 at 05:53