Lazycoder

5Jul/053

A quick tip when dealing with resources

A quick tip, this may already be well known around the community, when dealing with .resx files and namspaces.

Make sure that the default namespace for your Visual Studio project is the same as the class you are trying to access resources embedded in your .resx files from. We kept running into problems if the two were not the same in one of our custom web controls. It appears that when .NET puts your resources into the manifest of your Assembly, it associates them with the default namespace of the project somehow. (I’m not entirely sure how this is all accomplished. I’ll see if either I or my co-worker can hunt down someone at MS who can give me a better explanation.).

The error you will get is long and generic, something about how the resource could not be retrieved for the given culture. I’ve just re-imaged my work machine, once I finish installing everything I’ll replicate the error and the solution, posting the code and error message here.

  • Hermann Klinke

    I am not an expert on this, but I have found out the following through trial and error (and looking at the source with reflector).
    resx files that are not associated with a WebForm or UserControl are named like this: …resx
    resx files associated with a WebForm or UserControl are named like this: .

    backslash (“\”) is replaced by a dot (“.”)
    any character that is not allowed in namespaces will be replaced by an underscore (“_”)

    for example “Resource Files\en(GB)\Data.resx” becomes “Resource_Files.en_GB_.Data.resx”

  • Hermann Klinke

    Alright, I try it again. I used tags for the variables which are appearently stripped from the comment.

    resx files that are not associated with a WebForm or UserControl are named like this: %ProjectName%.%FolderName%.%ResourceName%.resx
    resx files associated with a WebForm or UserControl are named like this: %Namespace%.%ClassName%
    backslash (“\”) is replaced by a dot (“.”)

    any character that is not allowed in namespaces will be replaced by an underscore (“_”)

    for example “Resource Files\en(GB)\Data.resx” becomes “Resource_Files.en_GB_.Data.resx”

  • Scott

    Good find, thanks!