Resources folder in Visual Studio - c#

I write you all for a problem with my c# library. It needs an XML file that uses as a dictionary. Since the files will not change I would like the dll already contained the file and requires no memory references to it.
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(#G:\Project\dictionary.xml");
So I put the XML file in the "Resources" folder of visual studio, but now I do not know how to get that file. If you always use the memory reference to use the library without also enter the file, it does not work. How can I do? If I open the dll with a notepad I see that the XML file is in.

This should give you an idea:
add a *.resx file
drag and drop you XML File onto it.
Use it!

Related

Adding resources to project from existing files

Simple and plain question as this is the first time for me working with resources. To my console application/C# I have an xml file that contains some data, then I chose to add this xml file as a resource to my project, the question is, if I once need to change the data in the xml file, does the resource file update accordingly?
Thanks!
Yes, files added as resource stay in they original format and can be changed via normal editors for that file format. Obviously you will need to recompile project to see the change in resources.
Note that by default source files will be copied into project (so you need to modify copy in the project folder), but you can explicitly add "as link" to refer to some other file on your local disk.

Modify c# Windows Forms control library

I have a c# Windows Forms control library, but there is no sln file with it.
I would like to modify this project, so I think I need an sln file to open it and create an other dll.
I tried a solution, but it did not work.
I create a new c# Windows Forms control library, I named it as the original library name.
I renamed the UserControl1.cs as the original mainfile name.
I bult it, it has worked, yet.
After that I copied all of the .cs files from the original library to the new library.
Of course overwrite the new files with the original files.
I tried to build it again, but I got an error message: "dll does not contain any usercontrol types"
The original folder contains a __csproj.user file, but my new project does not contain it.
What did I wrong? How can I modify the original c# Windows Forms control library oe how can I create an sln file for it?
If you don't have .sln file, that's fine visual studio will create it for you when you open a .csproj file and try to save the project.
All you have to do is open the .csproj file in visual studio and save it.

Add a file to project resources using C#

I'm creating SpecFlow tests for an application that uses an xml settings file (example, C:\ssis\mySettingsFile.xml) to run. In one test, I want to save the file to disk, and then add that file to my project resources and clean up the disk location. Then, another test will unpack the resource to a temporary directory and use it from there.
I'm clear about the unpacking part, but is there a way to programatically pack the file into a project resource rather than manually adding it to the project using the VS GUI and marking it as an embedded resource?
I know this is wrong, but I'm thinking something along the lines of:
string myPath = "C:\ssis\mySettingsFile.xml";
TestHelper.ResourceDirectory = "$\...\...\Project.Folder.Resources";
myResource = TestHelper.PackResource(myPath);
myResource.IsEmbeddedResource = true;
...where PackResource method saves the file to the project resources.
Thanks in advance!
you can create a Resource Folder and add your xml to it.
When you click on Properties of the project, there will be a Resources tab wherein you can see your file.
To access the file, you can use ProjectNamespace.Properties.Resources.yourfilename.

How do I embed my icons, and resource files into my EXE while using Visual Studio 2012 and C# [duplicate]

I would like to embed a text file in an assembly so that I can load the text without having to read it from disk, and so that everything I need is contained within the exe. (So that it's more portable)
Is there a way to do this? I assume something with the resource files?
And if you can, how do you do it and how do you programaticaly load the text into a string?
Right-click the project file, select Properties.
In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
Then from the toolbar above the tab-page, select to add a new text file, give it a name, it will be added to your project and opened up.
If you get this far, then in your code you can type in Resources.TheNameYouGaveTheTextFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).
If something was unclear about the above description, please leave a comment and I'll edit it until it is complete or makes sense :)
In Visual Studio 2003, Visual Studio 2005 and possibly earlier versions (this works in 2008 as well) you can include the text file in your project, then in the 'Properties' panel, set the action to 'Embedded Resource'. Then you can access the file as a stream using Assembly.GetManifestResourceStream(string).
Other answers here are more convenient. I include this for completeness.
Note that this approach will work for embedding other types of files such as images, icons, sounds, etc...
After embeding a text file, use that file any where in code like this...
global::packageName.Properties.Resources.ThatFileName
Here's what worked for me. (I needed to read contents of a file embedded into an executable .NET image file.)
Before doing anything, include your file into your solution in Visual Studio. (In my case VS 2017 Community.) I switched to the Solution Explorer, then right-clicked Properties folder, chose Add Existing Item and picked the file. (Say, FileName.txt.) Then while still in the Solution Explorer, right-click on the included file, select Properties, and pick Build Action as Embedded Resource.
Then use this code to read its bytes:
string strResourceName = "FileName.txt";
Assembly asm = Assembly.GetExecutingAssembly();
using( Stream rsrcStream = asm.GetManifestResourceStream(asm.GetName().Name + ".Properties." + strResourceName))
{
using (StreamReader sRdr = new StreamReader(rsrcStream))
{
//For instance, gets it as text
string strTxt = sRdr.ReadToEnd();
}
}
Note, that in this case you do not need to add that file as a resource as was proposed in the accepted answer.
Yes, you are correct - create a resource file. WHen you do that you don't need to "load" the string, it will be referenced as Resource.WhateverStringYouDefined.
Here is what I did:
Added my files (resources) in Visual Studio by right-clicking on the project.
Right click on every file you have added and change the "Build Type" to Embedded Resource.
In order to access the resource:
a. Got the current assembly using the function: GetExecutingAssembly()
b. The resource that I added was a text file so I read it into a stream using GetManifestResourceStream(fileName). The way I accessed the file names is by calling GetManifestResourceNames()
c. Now use a StreamReader() class to read to the end of file into a variable if that is what you want.
Adding to Pavan's answer, to get the current assembly (in general section):
Assembly _assembly;
GetManifestResourceStream(fileName)(in code, where the read from resource is required):
try
{
_assembly = Assembly.GetExecutingAssembly();
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("*Namespace*.*FileName*.txt"));
}
catch
{
Console.WritelLine("Error accessing resource!");
}

C# - Add File as Resource to my Exe at Runtime

I discovered that I can add Files (*.jpg) to my C# Resources in Visual Studio 2010. As far as I could read it should be possible to re-assemble the exe at runtime. I don't want to strip the exe apart by myself. I'm looking for C# routines that do that for me. Of course I don't want to modify the running exe but a copy of it. I could also live with it putting my source code inside my exe if I need to compile it again at runtime.
My goal:
Create copy of the running exe
Add a file to that copy somehow.
Close the running app
When user executes the copy it must have the file as resource inside. That's it.
Edit: C# compiler + Visual Studio 2010 is available at target system.
(I'm not programming a full installer, please don't say those bad words: "re-inventing" and "wheel", I know them by myself)
You'd have a very hard time modifying your resource section in a way that won't break your executable without the benefit of a full compiler.
What you can do instead however is:
Make a ZIP file (or other archive that contains all your files)
Do a dumb-append of the contents of your ZIP file to the end of the executable
Also append an int32 containing the length of your archive
You can read it by opening a FileStream for your own executable starting at ExecutableLength - ZipLength - 4 and reading ZipLength bytes - which gives you just the zip portion which can be read using DotNetZip or another library.
Then when you want to modify the stored data:
Rename your existing executable while running (which you can do)
Read the first ExecutableLength - ZipLength - 4 bytes of your executable and write them to a new file with the name your executable had originally before it was renamed.
Create the new ZIP archive with your information in and append that.
Append the int32 of the length of your new archive.
Close the existing app and launch the new one.
Tadah - an executable that can modify its own stored resources.

Categories