Where do I put .resx files? Sometimes I see these files under Properties folder. Is there any design guideline about it?
Thank you!
Depends on what you are doing.
For example a web Application they go in App_GlobalResources or App_LocalResources folder.
For other projects I would create a Resource folder and put them there.
In a WinForms app, the .resx file associated ot a class (form, user-control,...) is stored side-by-side with the source code (e.g. C#) file. In addition, a global .resx file is created in the properties to let you store global stuff such as messages, pictures,...
If you right click your project in VS solution explorer you should have an option "Create folder" this will then have a list of folder you can create. App_GlobalResources and App_LocalResources is another.
For project wide strings and resources add the contents to the App_Global forlder. for form specific resources add the content to the App_Local folder instead.
Have a look here for more information
Related
Im currently trying to add 2 files to my project. One of them is a .ovpn (vpn Config file) and the other is a batch file.
Im trying to create a directory in my code and then add these files to that newly created directory so when the users first run's this application it creates the files needed to run the application.
Is there anyway of adding these files like a resource so I can just reference them easy?
Sorry if this is a really simple question. Just never done it before.
Thankyou in advance!
You can try to change properties of your files like:
You don't need to necessarily have them embedded in the executable to have them easily referenced.
Add a 'resources' type object to your project - call it Resources.res
Right click on your project, make a New Folder
Call it 'Resources' - add your resource files in here.
Double click on your Resources.res file
Click on the 'Add New Existing File'
Navigate to your file that is in the Resources directory and select it.
You should see your file showing up inside the resources viewer now.
When you are writing code, you can now reference your added files via:
Resources.<nameoffile>
If your file is a text file, you can do things like
string jsonRequest = Resources.RegistrationRequest;
This should automatically set the files to be embedded as well, so they should be accessible at run time.
I have an app on Windows Embedded which uses .resx files to translate the app to different languages.
Also I create an installation .cab file but I can't include the resx file to this cab.
How can I achieve this?
Thanks for any tip
A few things:
You'd not told us how you're trying to add the file. Are you using a custom INF file and just calling CABWIZ or are you using a Visual Studio Installer Project?
What have you done to try to include the file?
Most importantly, a RESX file does not contain the run-time resources and you rarely would deploy it. The RESX resources get compiled into a *.resource.dll assembly, that is typically in a subfolder with a name for the locale (e.g. en-us or fr-ca). You need to deploy those files/folders which is challenging because CABWIZ doesn't allow duplicate file names (and all resources have the same file name, just different folders). That scenario is handled by this SO question.
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.
I have a problem with resource files.
I have a solution with two projects. The first project contains ImageResource.resx file with the images that I use. Every Form in this project can access this file from the designer. But I can see in the designer ImageResource.resx file to use it from second project (Reference to second project is present).
I have added the ImageResource.resx file as a link to my second project. And I saw it in the designer! But when I use an image from this resource in the second project Visual Studio modified my original file (It sets the namespaces, and other..) and my solution breaks. Also Visual Studio tells me that ImageResource.resx is present in two dll's first_project.dll and second_project.dll
Can anybody help me with How to correctly use shared resources between projects?
The correct way to share resources is to create a global shared project. Create a new Project with the name Resources:
Next we must add some resources (such as icons) to the project. Do this as usual. Go to the projects setting, select tab Resources and Add Existing File… to the project. We can see that the icon is added to the project and was copied to the local folder:
Next step consists of adding this icon to the other project(s). Note the important difference, you need to add this icon as a link!
Adding as a link avoids the resource duplication. Create a new Project within the same solution and name it e.g. Main. Create some folder in this new project, naming it Resources (the logical name for our purpose). Then right click on this folder, select Add Existing Item… and choose the image file from the shared project folder. Make sure to use Add As Link here! If done correctly the icon of the newly added file will look slightly different (see below):
Added resource's icon must look like this
Now we must set the Build Action for this file to None. For this select the file and go to the Properties Window. There choose None for Build Action. We need to do this to avoid embedding this icon into the assembly:
Finally we need to add the linked files to the Resources of the corresponding project. Open the project Properties for the project where we just added the files. Choose the Resource tab and drag the linked file there:
These are the five simple steps you must perform to share icons between projects. You might ask "What are the benefits of this?" The benefits are:
We store resources in one place, and
It is easy to replace an icon with a new one.
This didn't work for me and I found another (VS2015+) approach.
See https://stackoverflow.com/a/45471284/4151626
In short, the shared project is directly included into the peripheral project. Thus, even though the IDE does not support <Resource> elements in the shared project. <Resource> elements can be added to the shared project, via a text editor. They are then incorporated into the peripheral project during the build.
(Apologies for the hyper-link. I would just repost the answer for clarity, but the stackoverflow editors crack down on this, deleting duplicate answers to save you from ???.)
Can you use a symbolic link to share the file into multiple folders?
windows:
mklink linked_location\ImageResource.resx original_location\ImageResource.resx
C:\Users\preet>mklink
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link specifies the new symbolic link name.
Target specifies the path (relative or absolute) that the new link
refers to.
If a resource file is really shared between projects, you should put it in a shared project.
Solution 'Sample'
Project Sample.Data
Project Sample.Business
Project Sample.UI
Project Sample.Resource //shared resources are put in a shared project
You can't see the resource if it is not public, and it is default set to "Friend". You can set the "Access Modifier" in the Visual Designer (upper right-hand corner).
Had a basic WinForm question: By default a resx file is created for every form or user control (along with the designer.cs). This resx works fine for all the controls and the text added to the controls via the UI.
I was wondering if I could use the same resx to add strings which have to be used programmatically and based on conditions, attached to the controls? Will the resx get overridden in any case and this custom strings be removed?
What is the best practice to follow in this case?
There's a strange problem with the string resources in the Resources.resx file. There's no obvious way that I ever discovered how to create a new resource table for another language with the IDE. It can be done by hand though. Follow these steps:
Project + Properties, Resource tab,
add the strings you want to use in
your program
Start Windows Explorer and navigate
to your project's Properties folder
Copy and paste the Resources.resx
file
Rename it to the culture you want to
use. For example:
Resources.fr-FR.resx
Back to VS, click the Show All Files
icon in the Solution Explorer window
Expand the Properties node, the new
resource file should be visible
Right-click it and select "Include
in project"
Select it, in the Properties window
set Custom Tool =
"ResXFileCodeGenerator"
Verify that Build Action is set to
"Embedded Resource"
Build your program. You should get a new folder in your project's bin\Debug directory with the satellite assembly, named projectname.resources.dll. This satellite assembly contains both the localized strings and any localized resource from the forms. Test that it works with code like this:
public Form1() {
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
InitializeComponent();
textBox1.Text = Properties.Resources.String1;
}
The auto-generated ones get overwritten (I'm using 2005), so no I would not use the same file. I would suggest creating a separate area in your project for .resx files like this. Usually I create one .resx per form, matching the name, of course.
Edit: Here is a more detailed answer I gave recently to organize the file/folder structure when localizing your app with .resx files.
Or you can try this:
Add this code in main();
using System.Resources;
ResXResourceWriter rw = new ResXResourceWriter("Resources.de-DE.resx");
rw.AddResource("String1", "de");
rw.Close();
...
repeat for more files
Go to the bin directory and move the XML (resx) file(s) to perhaps the property folder.
Go to Step 5-9 above in nobugz post.