Difference between having resources in Resx file and folder - c#

I know I can add resources to Resources.resx file and then use it like properties.resources.MyFile.dat.
But what is the difference when I simply add file to Resources folder (setting it to embedded) and Copy to output dir to Always copy and access it like "\Resources\Data\file.dat"?
Also this is another way how to add resources?

There are two core differences:
When you have your resources inside a .resources file, they are normally embedded on your assembly (the .exe or .dll file) instead of having their own file.
When the resource is embedded on your assembly, you can rely on the .Net Framework's localization infrastructure to have localized versions of it if the need arises.
In summary:
For resources you'll need to localize, embed.
For resources you'll need to change from the program, use stand-alone files.
For everything else, it's just a matter of taste.

Related

Can't add a .bin.gz file as embedded resource

If I include a .bin.gz file in a DLL as an embedded resource, it doesn't show up in the Assembly.GetManifestResourceNames() list, and cannot be loaded with Assembly.GetManifestResourceStream(). If I rename the same file to anything else, it does show up.
The .bin.gz file shows up in .csproj. I'm building a .NET Core 2.2 class library.
What's going on? Is there some kind of filter by file type that prevents certain types from being embedded?
EDIT: On further inspection. It appears that the .bin.gz resources are placed in a different .dll named [AssemblyName].resources.dll, which is placed in a "bin" folder next to the [AssemblyName].dll. Why is this and why does this not happen with other files?

Create single Exe file from WPF project

Is it possible to create single exe file from my C# WPF project? The project contains some images and videos in Movies folder inside bin.
How can I compile all of this in singe exe file? I know it will be large in size but it is OK.
You should add this files to you project and use Build Action: Resource for this files. In this case, it will be introduced into the main assembly of application.
It will be a binary resource, because it is embedded in the compiled assembly as an opaque binary large object.
Quote from Matthew MacDonald book: Pro WPF 4.5 in C#:
There are a couple of things that you must not do in order to use assembly resources successfully:
Don’t make the mistake of setting the Build Action property to Embedded
Resource. Even though all assembly resources are embedded resources by
definition, the Embedded Resource build action places the binary data in another
area where it’s more difficult to access. In WPF applications, it’s assumed that you
always use a build type of Resource.
Don’t use the Resources tab in the Project Properties window. WPF does not
support this type of resource URI.

what will happen to images in the Resources directory once the app is packed and how safe are those images for the app?

the Resources folder typically contains all the app images (those 16 X16) that we use to look our apps good. I haven't dealt with installation and resources folder before therefore :
When application needs to be deployed, what would happen to those images in that Resource folder?
I haven't tried this before so when I install the application where would that resource folder be saved?
If the installation creates another resource folder to keep those images, then what if a user manually deletes that folder? App would crash? what is the solution to overcome this?
I've heard some place the images in a .dll file, is this a common practice?
thanks
When you build your application, Visual Studio invokes the resgen.exe tool to convert your application resources into an internal class called Resources. This class is contained in the Resources.Designer.cs file which is nested under the Resources.resx file in Solution Explorer. The Resources class encapsulates all your project resources into static readonly get properties as a way of providing strongly-typed resources at run-time. When you build through the Visual C# IDE, all the encapsulated resource data, including both the resources that were embedded into the .resx file and the linked files, is compiled directly into the application assembly (the .exe or .dll file). In other words, the Visual C# IDE always uses the /resource compiler option. If you build from the command line, you can specify the /linkresource compiler option that will enable you to deploy resources in a separate file from the main application assembly. This is an advanced scenario and is only necessary in certain rare situations. A more common scenario for deploying resources separately from the main application assembly is to use satellite assemblies as discussed below.
All your queries is answered above
i.e.
When application needs to be deployed, what would happen to those images in that Resource folder?
I haven't tried this before so when I install the application where would that resource folder be saved?
If the installation creates another resource folder to keep those images, then what if a user manually deletes that folder? App would crash? what is the solution to overcome this?
I've heard some place the images in a .dll file, is this a common practice?
For More information
http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx

VS2010 (C#) Environment.SpecialFolder.ProgramFiles is where in the Solution Project?

I have created a C# WinForms application. It has some additional files that it uses, such as help files and some external data files. I want to put these files in folders under the Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) path so that the application can find and read them.
This path doesn't get created until the application is installed right? So where do I put these files in my VS2010 project, or how do I tell my project that these files exist so that when I am running (debugging) the application from VS it will find the files.
Thanks
EDIT: I did think about adding the files as resource files, but unfortunately some files may get added after the project is built and deployed. Therefore we decided to make the program search and find the data files (and associated help files) at a specific location and load them if they exist.
Thing is that your application should not need to use 'Environment.SpecialFolder.ProgramFiles'. It "should" be location agnostic. The quick answer to your question is "from the folder that the application was launched from". The real answer is how you reference these files.
Your help files and data files need to be deployed to folder with a known relationship to the application. That is, the same folder or a child folder. Consider making the file resources.
Now if the files are user configurable or run time writable then they should not be in the program files area but be in the application data area. If so, then there is your path!
Hope this helps.
You should add these files to the main (exe) project inside your solution.
(Right click on Project Name, Select Add Existing Item)
Then set its property Copy to Output Directory = Always
(not sure of the translation because I use a localized version of VS)
Of course, it should be noted that, when you deploy your files in the Environment.SpecialFolder.ProgramFiles, you could not be able to write to them (Windows7, Vista) for security reasons.
Add the files as resources in your project. Take a look at this MSDN article. You can add resources to a project by right-clicking the Properties node under your project in Solution Explorer, clicking Open, and then clicking the Add Resource button on the Resources page in Project Designer.
You can add resources to your project either as linked resources, which are external files, or as embedded resources, which are embedded directly into the .resx file.
When you add a linked resource, the .resx file that stores your project resource information includes only a relative path to the resource file on disk. If you add images, videos, or other complex files as linked resources, you can edit them using a default editor that you associate with that file type in the Resource Designer.
When you add an embedded resource, the data is stored directly in the project's resource (.resx) file. Strings can only be stored as embedded resources.

Include and Reference Resource File from C# class

I have an image that is used in some PDF files that my C# application generates. I know how to reference the image file when it is located in my workspace, but when I compile the program, I don't see the image anywhere in the compiled directory.
Can someone tell me what happened to that file, or do I have to manually package the file along with my program when I send the program to the users? I added the image to the workspace by drag-drop to the resource directory of one of my namespaces.
Check the file's properties in Visual Studio. Have you set the CopyToOutputDirectory property to something besides Do not copy?
Hmm... I'm not sure about the whole drag-drop business, but if it's all working the resource will have been embedded into your assembly.
I suggest you have a look with Reflector - you can see the resources embedded within assemblies using that.
Have a look at the build properties for the item in Solution Explorer - in particular, if the build action is Embedded Resource then it will indeed be built into the assembly.

Categories

Resources