VS/C# Equivalent of Java/Eclipse "resource folder"?

Viewed 424

While using Java in Eclipse IDE, one can add a folder to the "Build Path" using the "Add Class Folder" option in the "Libraries" tab, which allows the resources in that folder to get compiled inside the application's jar file, rather than outside or not at all.

Click here to view an example.

With this, one can get the resources inside the folder as a URL via the Class.getResource(String name)method. I am already informed about C#'s equivalent: Assembly.GetManifestResourceStream(string name) or Assembly.GetManifestResourceInfo(string resourceName) methods, but I am not aware of C#'s "Build Path" equivalence in Visual Studio (I am using 2019, if you wished to know). Could somebody please explain how I would accomplish Java's build path resource folder in C#?

(Note that I am looking to create a resource folder where anything put inside the folder would be considered an application resource. I am not looking for a way to add one or more resource files individually.)

Any replies would be greatly appreciated! :)

2 Answers

After a little research, I had found a solution for this problem. There are in fact two possible solutions to this issue.


.NET Core Solution

The first involves editing the .csproj file of your C# project. This solution is only available in .Net Core.

You can add this code snippet to your file and change the {PATH_TO_RESOUCE_FOLDER_HERE} folder to your desired folder.

<ItemGroup>
    <EmbeddedResource Include="{PATH_TO_RESOUCE_FOLDER_HERE}\**" />
</ItemGroup>

Now any item placed in that folder will be considered an embedded resource Assembly.GetManifestResourceStream(string name) method.


Regular .NET Solution

The second method involves using a .resx file to encapsulate all of your resources

In Visual Studio 2019, you can create a .resx file by right clicking on the location in your project where you wish to add the file to, and navigating to Add > New Item (you may also press Ctrl+Shift+A). You can now navigate to the item that quotes "Resources File" and select it. You can now use this GUI to insert your resources (for a deeper explanation, click on this or this link. For use cases, see this MSDN).

The "Resources File" option

Note that this solution will also work in .NET Core.


I hope this answer helped you as much as it did me! :)

You just create a folder and name it as you like it, say 'Resources'. Add any file you want in there to be treated as a resource by your application.

Then navigate to the properties of every resource file (you can press F4) and in the menu you can choose what you want the compiler do with that file (Compile Action is the option name if I remember well). There you select the type as a resource, the namespace (your Build Path), and whether you like the file to be copied every time you compile your application, and so on.

Related