Windows Form Localization - c#

I'm trying to get localization to work in my application. I can follow the typical example online and get it to work in a sample project. (ie. setting the Localizable property and the cultures).
When I try to use it in my application, it always uses the default resource file no matter what language I choose.
EDIT: If I go to the properties of my startup project, select Debug, and set the Start Action to Start Project, it will work. If I run the executable that gets created when building by selecting it in "Start external program", it will not work. Any idea why?
EDIT: When I use the Start External Program, the executable I am pointing to is not pointing to the bin/Debug folder of the project. I have a post build event to copy it elsewhere and am pointing to that. Because of this, maybe it is not able to see the reference files that are in the Debug folder?

When you localize a form, it creates a folder per language in the build's output path that holds a resource file projectname.resources.dll.
I have a post build command that puts our .dlls elsewhere. I needed to change the command to also push out the resource files so that they were in the same directory. Once I did this, I was able to get it to work.

Related

How to copy built assemblies into a project resource folder?

In the Resources folder of my project, I have many different DLLs from other assemblies in my solution. These DLLs are used as embedded resources so I can have a single EXE without having to have local copies of all the DLLs. What I want to be able to do is have them updated every time I build my entire solution. So for example, if I have AssemblyOne/bin/x86/Debug/Foo.dll, I want the DLL to always copy over to LocalAssembly/Resources/Foo.dll whenever I build the project.
Is there an easy way to do this? I was looking at the post-build-event macros, but none of them would be able to directly reference my "LocalAssembly", which is not the Solution Directory. Also, I'm not sure how I would go about writing a new custom post-build command in a way that the newly copied DLLs would not be an absolute reference to my local machine. Thanks! Please comment for more information and I will edit the question.
You need not access only your solution folder in post build events. The post build scripts run with whatever permissions the compiler had when it ran. Since in Visual Studio things frequently run with Administrator permissions, chances are great you have access to your entire file system. As a result, if you are wanting to copy resources around, you merely need to presume that you are starting the xcopy call in the build destination directory. From there, you can navigate around with normal paths. So if, for example, you needed go up three levels and then into the directory LocalAssemblies, your copy command would look like xcopy Foo.dll ../../../LocalAssemblies.

Where do I put a folder that is used in my C# application?

I'm planning on my C# application having a folder called Data that is reused whenever the program is launched. I put it in the root of my Visual Studio solution which causes it to be where you see below
MyProject (File folder)
bin (File folder)
Debug (File folder)
MyProject (Application)
.
.
Data
obj
.
.
.
.
after the project is built ... but I'm guessing I'd want it where the actual application is, right? Or is it proper for me to instead have a "Create if doesn't exists" for that folder in my application? Sorry if this is considered a subjective question.
1) you may check it at the start of the program if it doesn't exist then you may create it.
Right click on your SOlution in visual studio>> Add new project>> Under other project types>> Choose>> Setup and deployment >> Visual Studio installer >> Set up project.
Setup projects are important in many cases, when you need to make any changes for first time installment or need to modify registry, need to copy some data folder or other settings for the first time.
2) Create a setup project and in your setup project you may create that data folder, which will be created when user will install the application.
I would prefer to use both option to avoid any issue in future.
From what it looks like in your example the data folder you created in your application/solution explorer. So far so good but it wont ever be created inside your project.
You have 2 options if you want it to be in the same directory as your application.
You create a .txt file or whatever you want to put into it later on in the solution explorer and say "copy always" in the properties
Is you create your folder manually inside your program. The base location of your program is usually AppDomain.CurrentDomain.BaseDirectory. Thus you could use Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + folderName).
For 2 this could be done on each run on the program (if the directory does not exist yet OR on setup of the program if you have a special setup routine).
As far as I'm aware all of these variants are legit and are dependent on your exact use case and personal taste.
I'm guessing I'd want it where the actual application is, right?
NO! Let's say you eventually bundle this program into an installer the puts it in it's own Program Files folder. Standard Users do not have write access to anything under the Program Files folder!. You just forced your program to require Administrator rights every time it runs.
Instead, you need to put your data folder in the Application Data special folder:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

How can I share resources across multiple projects within a C# solution?

I have multiple projects in a solution and I'd like them all to share one pool of graphics resources. I've tried this:
1 - Created project1, made its resource file public, added some graphics to it.
2 - Created project2, Alt+dragged Resources.resx from project1\Properties to project2 (not in the Properties folder)
3 - Add reference in project2 to project1
So, now all the images from project1 are available in project2. So far, so good. I can use them at design time just fine.
But, when I want to access them at runtime, I try this (in project2)...
Image img = project1.Properties.Resources.image14;
And that crashes with a MissingManifestResourceException.
What am I doing wrong here? Is there a better way I could approach this? All I'm trying to do is maintain all my graphics in one place, so if I add a resource, it becomes available to all projects in the solution.
Just built an example following these steps:
Create a class library do hold the resources (Project 1)
Create the consumer project (Project 2)
Add a resource file (GlobalResources.resx) in the Project 1 and add a resource item Information
Change the BuildAction of the resource file to Embedded Resource
Change the Do not copy of the resource file to False
Check if the Custom Tool of the resource file is set to PublicResXFileCodeGenerator
Add a reference to the class library (Project 1) to the consumer project (Project2).
Add the resource namespace reference wherever you want to use it.
Finally it is working: GlobalResources.Information
It should be simple.
Edit:
You are concerned about using an external resource file inside the design time property editor. Sorry to inform that there is no standard support for this :(
However, if you think that the benefits are greater than the effort:
Issue with shared WinForms resources across projects in Visual Studio
How do I get the Windows Forms Designer to use resources from external assembly?
Hope it helps.
Choose the referenced file in your solution explorer, then properties, then see what the "copy to output" property looks like. I suspect it's not set to "Copy Always" or "Copy if Newer" of which either should be fine.
Once it's being copied, let's also check to see where it's being copied. Is the output path for that particular item the same as where the program ultimately expects? Is it being copied to the bin\Debug of the correct project?
Make sure it's being copied to the path where the MissingManifestResourceException says it's failed to find the resource.
Finally, given additional information in our comments, I would also suggest you verify the following:
culture the resources are targeting. Check spelling and capitalization.
any culture settings of your build xml or publish xml.
culture setting(s?) of your host system that's running this code.

Where can i find an executable program once i have finished writing c# code

In visual basic class I learned where I could find the .exe file once the program was done and run at least once. Basically we could take the icon for the .exe file and place it on the desktop so that a user could just double click on the icon to run the program without needing to open the IDE or look at any code.
Where/how can i find this kind of file for c# code?
Go to the bin/Debug folder in the project. (or bin/Release if you're using the release build).
You can also go to your project settings, then to the build tab, and in the "Output" heading read/change the "Output Path" setting. This will let you output the exe to some other location, or just see where it is currently outputting in the event that it has already been changed on your machine.
Assuming you're using a web application project you'll use the .aspx file that is generated to access your silverlight application.
The "application" so to speak is actually a file ending in ".xap" that can be found in your web project's ClientBin directory.
The .xap file can also be found in the silvelright project's Bin
If you're wanting to install the silverlight application to a desktop you'll have to enable the ability to run the application "Out of Browser" and it'll have to be installed. More information on Out of Browser apps...

Access project files from class in separate folder

Screenshots below are from the Solution Explorer Pane:
I have a class in Folder DataAccess that needs to access a resource in Folder data. I get the following error message.
System.IO.IOException: Cannot locate resource 'data/danio_rerio.xml'.
Does not Work If:
It works if:
Folder data is a child folder within Folder DataAccess. Is it possible for me to use the following syntax:
foldername/somefile.someextension
to access resources within the project that are in seperate folders?
Edit: I do not want to hard code locations to my directories.
When you run your application, the running directory becomes your "active directory"
Suppose you have
c:\myproject\bin\debug\myapp.exe
When you run the application you try to search
c:\myproject\bin\debug\foldername\somefile.someextension
what you actually want could be close to
c:\myproject\data\daniorenio.xml
so you'll want to search the following directory
c:\myproject\bin\debug\..\..\data\daniorenio.xml
The .. operator tells that you want to go back in the directory hierarchy
However this is assuming your binary will be in a lower branch than your ressource. What I actually do is copy everything I absolutely need for runtime inside a special directory such as
c:\myproject\RuntimeRequired
This way I can issue a post build event like this (In Project/Properties/Build-Events/Post-Build)
copy /Y "$(ProjectDir)RuntimeRequired*" "$(OutDir)"
I found the answer myself. Should have searched more before offering a bounty -_-
Pack URIs in WPF [Source]

Categories