38

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

2 Answers 2

78

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;
4
  • 27
    Sorry, I will bubble wrap it next time.
    – Gabe
    Commented Oct 3, 2013 at 18:17
  • 13
    @Gabe While I agree that the answer is good and acceptable, I also find that it would have been more insightful by explicit showing the type instead of using var.
    – tobiak777
    Commented Jul 2, 2015 at 9:06
  • When in same namespace it could be left out. Commented Jul 9, 2017 at 9:55
  • 7
    What is the return type from My_Zip_File? Is it a path string, binary, stream or Path object?
    – chris31389
    Commented Aug 20, 2018 at 11:33
9

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

1
  • It works well for me when I set the 'Build Action' to 'Content' in the mentioned step 2
    – sree
    Commented Oct 9, 2023 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.