Is there any way to add a file to an application's resources using code (not the visual designer)? I've tried the ResourceWriter class but it doesn't do what I want.
The ResourceManager class is only used for getting resources. If you want to add or edit resources, see the MSDN page on Adding and Editing Resources, or look at the Resource File Generator for dynamically creating resource files.
Edit: The ResourceWriter class will probably do what you want, too... did you read the remarks on how to use it? Generating resource files is simple, you would just have to programatically insert them into your project if that's where you wanted them ultimately.
Related
Good day!
I have some file called "*.dat" with text into it.
So, i try to create attribure "Version" ,but don't know how.
Can i do it via c#? Can you write some examples?
Such like this?
File.SetAttributes(path, attributes);
Thank you!
You can't. The only values allowed are from the list here
You're probably struggling because you can't add arbitrary information into a file. There are a known set of attributes you can change using the FileAttribute properties
What you would normally do is provide some information at the start of your file, typically called the file header. This then allows a custom reading implementation to read out the version, without having to read the rest of the file. This is quite standard practise with all the files you're used to, for example a WAV audio file:
The attributes you can change here are the NTFS attributes. Details here:
http://msdn.microsoft.com/en-us/library/system.io.fileattributes%28v=vs.110%29.aspx
Version is a resource embedded in an executable file. A similar question was asked here:
How do I set the version information for an existing .exe, .dll?
reating "custom" file attributes is not allowed.
You can only set one of the FileAttributes .
Example:
File.SetAttributes(#"C:\myfile.txt", FileAttributes.Hidden);
I've working on a project where I'm using ResourceManager extensively and this question just crossed my mind.
Can we read from .resx files without using ResourceManager? I mean, is there another way?
ResourceManager is a convenience class, it works very well with the way the build system supports .resx files. No, it is not a strict necessity.
A .NET assembly has the generic capability of embedding arbitrary data into the manifest of the assembly. Just a blob of bytes, it can be anything you want. Directly supported by the build system as well, just add a file to your project and set its Build Action to "Embedded Resource". At runtime, you retrieve the data in that file with Assembly.GetManifestResourceStream().
You can stop right there, but that's just a single file, it doesn't scale very well if you have many small resources you want to embed. Which is where a .resx file starts, it is an XML file that contains resources in a friendly format. One that gives you a fighting chance to recover the source again when the original got lost.
But an XML format is not a very good format for resource data, it is bulky and it is expensive to find data back. So .NET has resgen.exe, a build tool that turns the XML file into a binary file, a .resources file. Compact and easy to find stuff back. And fit to be embedded directly as a single manifest resource.
What you don't want to do is having to read the .resources data yourself. You'll want to use a helper class that can find specific resources back from the blob of bytes. You want use the ResourceReader class, its GetResourceData() lets you specify the resource name and it will spit the resource type and data back out.
You can stop right there, but an app often has a need for different sets of resources. A very common localization need. Which is what satellite assemblies are all about, different assemblies that contain nothing but resources, each for a specific culture. They are separate so you don't pay for the virtual memory that's required to store all the localized resources when you need only one set of them. What's needed here is a helper class that automatically locates and loads the correct satellite assembly and retrieves the resource for you, based on the current culture.
That helper class is ResourceManager.
If you choose to skip the use of the ResourceManager you can let Visual Studio handle code generation for you. Ultimately the generated code uses a ResourceManager, but you're no longer writing that code manually. Additionally, you get compile-time checking since you're referencing a generated static class.
If you add a resource file to your project and double click it from the Solution Explorer, Visual Studio presents you with a dialog where you can enter a name for a resource, and its value. The dialog presents you with options to add resources as strings, images, audio, etc. (look at the dropdowns at the top of the dialog). Next, to get the code generation bit, you need to set the Access Modifier to either "Public" or "Internal". The third option is "No code generation."
For example, add a resource file called "MyResources", then add a string resource with the name Greeting and a value of Hello! With one of the former two options selected for code generation (start off with public to test it, restrict the access as needed), you should now be able to reference the resources from your code via MyResources.Greeting. If you don't see it right away, make sure you've saved the file and try compiling.
string greeting = MyResources.Greeting; // "Hello!"
If you add other resource types (image, audio, etc.) then the return types will differ, of course.
At this point you could inspect the generated .cs file and see that the generated code is using a ResourceManager. The other use for resource files is localization. Let's say you wanted a Spanish version of MyResources. You would add a new file called MyResources.es.resx, where es corresponds to the language code desired (Spanish in this case). Now add the same resource name of Greeting with a Spanish value of Hola!.
If you change the thread culture to Spanish, referencing the resource will now return the Spanish version:
string defaultGreeting = MyResources.Greeting; // "Hello!"
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("es");
string spanishGreeting = MyResources.Greeting; // "Hola!"
Note that you only really need to set the access modifier to one of the code generation options for your default resource file (i.e., MyResources.resx), not for all the other localized versions you add. There's no harm in doing so, but in my opinion it's cleaner to have the main file generated while the others just have the resource values desired without code generation.
Well, Resources are compiled into the assembly. You could try to read the assembly by reading the bytes (or the IL), and extract the resources from there.
ResourceManager does this all for you, so I could not think of any reason you want to do this... Maybe one, if you don't want to load the assembly in memory, you could do it without ResourceManager.
Ref Microsoft: Represents a resource manager that provides convenient access to culture-specific resources at run time.
I expect, I'd you use multi Lang, you will get a more consistent result and better compatibility.
IMHO
I'm using MS Visual Studio Pro 2012 and I want to create some kind of help file.
I was thinking in create a html file like this but my question is: Do I need to have the html file always in this directory, even after I have the .EXE file created or the html file is added to the .EXE file?
If not, how can it be done?
[.NET Framework 4.5 | Windows Forms]
EDIT : I want to load a given (local) html file in the default web browser. This file should be 'inside' the .EXE file.
If you're looking to build a help file from Visual Studio, why not look at:
http://shfb.codeplex.com/
Sandcastle will build your help file based on the comments you have written on your classes and methods. Hit the forward slash three times (e.g. /) above your class or method declaration and the comment box will appear. Populate with salient details, run Sandcastle, and your help file will be generated.
The advantage of having a separate HTML file is that you can update it on it's own without pushing out a new assembly. However if you want to build it into the EXE, you can go to your project properties, then click on Resources. Add an existing file (your HTML file) and it will now be accessible from your code.
When you want to open it you can do something like this
string html = Resources.MyHelpFile;
if (!File.Exists("tmpHelp.html"))
{
using (var tmpFile = File.CreateText("tmpHelp.html"))
{
tmpFile.Write(html);
}
}
Process.Start("tmpHelp.html");
You can then delete the help file at a later stage such as when the user closes your application.
I'll recommend using HTML Help Workshop to create the help file. and then use Help.ShowHelp();. Its a lot more easier
But for your case. You can either do as KeyboardP suggested or move the file to your bin/Debug folder and then use
Process.Start("helpname.html");
NOTE : You'll also need to add the file to the Application Folder when you're creating your setup.
You can build html file (I think the most easy way it's to create it via microsoft word and to save as html)
Then you make a new form contain webBrowser tool and set the URL to your html file path, like this:
string filepath = Environment.CurrentDirectory + #"\Help.htm";
Uri uri = new Uri(filepath);
webBrowser_Help.Navigate(uri);
I'm a VB.NET programmer, but I'm new to C# and now I'm working with C# project which uses local resource files (.resx).
Using VB.NET I can access to variables in resource file via My.Resources.< LocalResourceFile >.< MyVariable > . But in C# I can't find any alternatives for My namespace, but I still can access to resource if I replace My namespace with < MyProjectNamespace >.
Maybe there are any other way to access my local resources?
If you absolutely must use the equivalent of the My namespace, there is actually such a thing in C#. It is the Microsoft.VisualBasic.Devices.MyServices namespace. To use that, you must add a reference to Microsoft.VisualBasic.dll and add using Microsoft.VisualBasic.Devices; to your code file. For this route, see here: http://msdn.microsoft.com/en-us/library/ms173136.aspx
I would not, however recommend doing that. Instead, to access resources, you simply use MyNamespace.Properties.Resources like this:
namespace SomeNamespace
{
...
var myFile = SomeNamespace.Properties.Resources.MyFile;
...
}
You can do this which is virtually the equivalent:
Bitmap image = Properties.Resources.picture000;
It still works the same way, just don't put "My.Resources" in front of the name. So if you added a .resx file named "Resource1" and put a string named "Foo" in the resource then the variable name is "Resource1.Foo".
If you put code in non-default namespaces then you may have to prefix it with the default project namespace. Like "WindowsFormsApplication1.Resource1.Foo". If you are lost then just look at the auto-generated code. In the Solution Explorer window open the node next to the .resx file and double-click the Designer.cs file. Don't edit it.
There is no built-in My namespace, but you can reference Microsoft.VisualBasic.dll and have it.
Check out: http://msdn.microsoft.com/en-us/library/ms173136(v=vs.80).aspx
We're building an application using WinForms (.NET 3.5)
I am familiar with the concept of Resources, however i don't follow the idea of default Resources per project.
In our project i see the usage of resources (RESX files) in the following contexts:
Per Project (Set in the project's properties under the Resources tab)
Per Control (Form for example) -- our main Form has an accompanying RESX file (expanded under the .cs file in VS)
What is the difference between the 2?
On one hand, having resources per form seems logically correct, but i cannot select Images for example from the specific form resource (only from the global one)
What is wrong here and how should the 2 be used?
If you want to use a resource in more than one form then using a project resource is best. That way it is stored only once. If not then keeping it in the form's resource file makes it easier to keep it them organized.
the forms ressources are used for lacalisation , if you configure the form to be localisable, visual studio will create a ressources file for each language : myform.en-US.resx myform.fr-FR.resx ....