c# add folders to resources?

…衆ロ難τιáo~ 提交于 2021-02-07 08:05:20

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/39732390/c-sharp-add-folders-to-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!