embed xml in custom controls - c#

i create a custom Control and using the XML file now i need to have just one dll file to use that to another project can you help me to embed xml file to my custom control project?
my programming language is c#.net

If you are using Visual Studio (My version is 2008, if you have 2010, this might be slightly different.)
Open properties for your DLL project.
Click the Resources tab.
Click the blue link (if there is one to add resources to your DLL).
Top Left with the little down arrow choose files.
Click Add Resource, find your file and add it. Give it a name like XmlFile with no dots, this is your handle to the file.
Save and Close Properties for the DLL.
Then somewhere in your project you will use this line, or something like it to access your file.
XElement root = XElement.Parse(Properties.Resources.XmlFile);

Related

Use solution-wide resources with WinForms

I have a Visual Studio 2017 solution, which is split in several C#-WinForm applications.
To have a similar user interface I put all the icons/pictures into one project (ResourceProject) and marked the resource file public.
By directly editing the .Designer-file of a Form I can now explicitly assign the Image to something from the resource file of the ResourceProject by stating
button1.Image = ResourceProject.Properties.Resources.DemoPic
However, from time to time, the designer overwrites this with
button1.Image = ((System.Drawing.Image)resources.GetObject("button1.Image")));
and creates a local resource file, which has the image embedded.
This is exactly what I wanted to avoid in the first place, since I might want to alter easily some icons later on and have those changes directly spread over all projects.
The only way to avoid it seems to be to assign the images not in the Designer file but in some other method in the actual form file.
Is there any other way around this problem?
You should not change designer.cs file, it's auto-generated and any change which you make in the file will be replaced with auto-generated code the next time which you change something in the form.
But to share image resources between multiple projects in a solution with design-time support, you can follow these steps:
Create a class library project, let's call it ResourceLibrary.
Add a Resx resource file to the root folder of the project with this name Resources.Resx.
Open the resource designer and change its Access Modifier to Public. (It will set its Custom Tool to PublicResXFileCodeGenerator)
Add a few images to the resource designer and save it.
Then in the Windows forms project do the following settings:
Add a reference to ResourceLibrary.
Right click on windows forms project and choose Add → Existing item...
Browse to the ResourceLibrary folder and choose Resources.Resx file.
Click on drop-down arrow of the Open button, and choose Add As Link.
Select Resource.Resx which has added to windows forms project and choose properties.
Set its Build Action to None
Set its Custom Tool to a text like None
Set its Custom Tool Namespace to the namespace of the resource in the other assembly: ResourceLibrary.
Rebuild the project.
Then for all the image properties you can choose the other resource file from drop-down in the Select Resource dialog. The code generation will generate correct code for the property and you have a correct build and it works as expected at design-time as well as run-time.
You can clone or download a working example here:
Repository
Download

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!");
}

Embed Text Files in a .NET Assembly or Executable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to embed a text file in a .NET assembly?
I have a c# winforms application that use several text files.
I would like to somehow embed those files either into the exe or dlls, so that they cannot be seen by the user when the application is deployed.
Can visual studio do something like that?
With text files you can also proceed as following:
Double click your Properties -> Resources.resx file to open the resource designer.
From the first small drop down on the top, select "Files" as resource type.
Click on Add Resource -> Add Existing File... and select your text files.
Let's say that the TXT you added was called Foo.txt... you can just access it's content like so:
String foo = Properties.Resources.Foo;
Then you can also change te file names inside the resource designer and they will be automatically refactored in your code. From the Resources directory, where they are now located in your project, you can also edit them.
I just recommend you to keep their access level as internal.
Here's how I do it:
In the solution Explorer, right-click on Properies/Resources.resx.
Choose "View Designer".
You can use the "Add Resource" button, but I often just drag the file right onto the designer window.
Visual Studio will perform code generation, such that you can access your file using Properties.Resources.YourFileName.
Be wary: Visual Studio will copy your chosen file into your project's Resources folder. It will not create a link to wherever the file was originally stored.

C# SciLexer.dll question

I am using ScintillaNET a wrapper of the Scintilla control. I've edited the a lexer a bit and would like to try and see if the changes work, but the problem is I don't know which SciLexer.dll file it's using on my computer. The one in the application's directory did not seem to make a difference.
So my question is basically, how do I know which SciLexer.dll file my application is using and how can I set it to use the one in the application's directory?
Using Visual Studio, check to see which DLL your project references:
With your project/solution open, expand the "References" folder in the Solution Explorer, and locate the name of the SciLexer.dll you are currently referencing.
Double-click on the relevant assembly to display it in the Object Browser.
In the bottom-right pane of the Object Browser, you will see the full path on disk to the specific DLL that your project is referencing.
This is the DLL that you need to change/update. Or, you could remove the existing reference and add a new one to the DLL in the directory that you want your application to use.

How to include non-compiled content alongside a C# program published via Visual Studio

I'm trying to publish an XNA game through the Visual Studio Publish tool. The game uses some compiled and some non-compiled content. Basically, I have a level loaded in via XML serialization and a short video. These two files are essentially streamed in, so they aren't compiled. The publish tool includes the compiled content fine, but any references to relative paths are broken because the CurrentDirectory for installed programs are set in the AppData folder.
I know that XNA can now compile XML without having to write custom content processors, but I don't particularly want to go back to rewrite that. I suppose I can if there's no other option, but that still doesn't remedy the video problem.
Is there any way to set up the publish tool so that I can do what I need to do? A setting or something? Or will I need to use a more fully-featured tool like NSIS?
Right click project, Add Existing Resource, browse and select the file you want to add. Then right click the file and click properties and change "Build Action" to content, and "Copy To Output Directory" to Copy if newer (or copy always if the need be). Then you can access it by using the relative path.
I use this for my XML and I can access my content using the following code:
XmlDocument document = new XmlDocument();
document.Load("Resources/DefaultConfig.xml");
Please note that my DefaultConfig.xml file is inside a "Resoruces" Directory which I created in Visual Studio(this is optional, but it helps me keep my project neat)

Categories