Bingjie Han Bingjie Han - 2 months ago 19
C# Question

c# add folders to resources?

In my project, I want to add several folders containing different files to

Project-Properties-Resources
, but I found that I could't add folders to resources, which is the way I really need.

So, is there any possible way that I can add folders to
Project-Properties-Resources
? In visual studio, I only found Add Existing File, Add New String and so on.

Thanks in advance to anyone who read my question.

Answer

You compress the folders into ZIP files, add the file, then decompress at runtime. using System.IO.Compression....

        string startPath = @"c:\example\start";//folder to add
        string zipPath = @"c:\example\result.zip";
        ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
        //add the ZIP file you just created to your resources

        //Then, on startup, extract the zip to a folder you control
        string extractPath = @"c:\example\extract";
        ZipFile.ExtractToDirectory(zipPath, extractPath);

To do this once per update, do something like create a setting for delete, set it to true on distribution, then:

 private void shouldExtract()
    {
        if (MyProject.Properties.Settings.Default.DeleteExtractionFolder == true)
        {
            if (Directory.Exists(myExtractionDirectory))
            {
                Directory.Delete(myExtractionDirectory);
                //unzip
                MyProject.Properties.Settings.Default.DeleteExtractionFolder = false;
            }
        }
    }

Adding a whole folder (with subfolders) as embedded resource?