How to access resource file in C#?

后端 未结 2 1375
谎友^
谎友^ 2020-12-15 16:07

i have add .zip file to my resource how can i access that zip file ?

相关标签:
2条回答
  • 2020-12-15 16:37

    Add your resource to the project in the project properties...(which you did)

    Then it's simple...

    var zipFile = MyNamespace.Properties.Resources.My_Zip_File;
    
    0 讨论(0)
  • 2020-12-15 16:53

    Visual Studio Community 2015 answer:

    1. In Project->Properties->Resources->Files, add My_Zip_File.zip, accessed by double-clicking Properties in Solution Explorer.
    2. Locate Solution Explorer->Resources->My_Zip_File.zip, click this item and look at Properties. Build Action="None" and Copy to Output Directory="Copy always".

    The file is now accessible programatically. The path and file is part of the assembly when debugging and when the progam is installed as a final publish. It compares favorably against using absolute path. Example C# header:

    using System.IO;
    using System.Reflection;
    

    Example C# code:

    String strAppPath = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
    String strFilePath = Path.Combine(strAppPath, "Resources");
    String strFullFilename = Path.Combine(strFilePath, "My_Zip_File.zip");
    

    ` For example, we can read the file as Stream and we can also open the file using strFullFileName.

    Source of reference

    0 讨论(0)
提交回复
热议问题