MissingManifestResourceException error - c#

I am trying to access a resources file which is in the same level as my .cs file.
The code I am using is as follows:
ResourceManager res = ResourceManager.CreateFileBasedResourceManager("Resource1", ".", null);
string s = res.GetString("String1");
However, this gives the following exception:
MissingManifestResourceException Could not find any resources
appropriate for the specified culture (or the neutral culture) on
disk. baseName: Resource1 locationInfo: fileName:
Resource1.resources
How could I resolve this?

resourceDir is name of the directory to search for the resources. resourceDir can be an absolute path or a relative path from the application directory.
so you can not make it relative to your .cs containing directory.

Related

How to iterate over Resources.resx in C#?

I want to access a number of files in C#, and I haven't been able to succeed in doing so without hard-coding a path. I do not want to specify an exact path, as the program should work independent of its location.
I figured I should do this by adding the files to Resources, but I can't find how to iterate over those. I found several pages about reading .resx, but it seemed like all either only addressed accessing one specific resource by name, or used a hard-coded path.
The code I have currently is as follows:
ResXResourceReader resourcesreader = new ResXResourceReader(Properties.Resources);
This gives the compiler error "'Resources' is a type, which is not valid in the given context".
When I do hard-code a path, curiously, I get an error at runtime.
ResXResourceReader resourcesreader = new ResXResourceReader(#"G:\Programming\C#\Contest Judging\Contest Judging\Properties\Resources.resx");
foreach (DictionaryEntry image in resourcesreader)
At the bottom line, an exception is raised:
System.ArgumentException
ResX file Could not find a part of the path 'G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png'. Line 123, position 5. cannot be parsed.
Inner Exception 1:
XmlException: Could not find a part of the path 'G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png'. Line 123, position 5.
Inner Exception 2:
DirectoryNotFoundException: Could not find a part of the path 'G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png'.
I wonder why it starts looking in bin\, as I did a Ctrl + F through Resources.resx and it does not occur.
You can do this by fetching the ResourceManager from the generated Resources class:
// Change this if you want to use fetch the resources for a specific culture
var culture = CultureInfo.InvariantCulture;
var resourceManager = Properties.Resources.ResourceManager;
var resourceSet = resourceManager.GetResourceSet(culture, createIfNotExists: true, tryParents: true);
foreach (DictionaryEntry entry in resourceSet)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
The resources are compiled into your application (or into satellite assemblies), so there's no resx file for you to load.

Copy file using relative path

File.Copy(#"my program\\subfolder\\what i want to copy.txt", "C:\\Targetlocation");
How can i copy a text file from one folder to another using relative path.
To execute the File.Copy the source and destination will be a valid file path. in your case the destination is a folder not File. in this case you may get some exception like
Could not find a part of the path 'F:\New folder'
While executing the application, the current directory will be the bin folder. you need to specify the relative path from there. Let my program/subfolder be the folders in your solution, so the code for this will be like this:
string sourcePath = "../../my program/subfolder/what i want to copy.txt";
string destinationPath = #"C:\Targetlocation\copyFile.txt"
File.Copy(sourcePath, destinationPath );
Where ../ will help you to move one step back from the current directory. One more thing you have to care is the third optional parameter in the File.Copy method. By passing true for this parameter will help you to overwrite the contents of the existing file.Also make sure that the folder C:\Targetlocation is existing, as this will not create the folder for you.
File.Copy(#"subfolder\\what i want to copy.txt", "C:\\Targetlocation\\TargetFilePath.txt");
The sourceFileName and destFileName parameters can specify relative or
absolute path information. Relative path information is interpreted as
relative to the current working directory. This method does not
support wildcard characters in the parameters.
File.Copy on MSDN
Make sure your target directory exists. You can use Directory.CreateDirectory
Directory.CreateDirectory("C:\\Targetlocation");
With Directory.CreateDirectory(), you don't have to check if the directory exists. From documentation:
Any and all directories specified in path are created, unless they
already exist or unless some part of path is invalid. The path
parameter specifies a directory path, not a file path. If the
directory already exists, this method does nothing.
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
try
{
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
}
You can provide the relative path from your current working directory which can be checked via Environment.CurrentDirectoy.
For example if your current working directory is D:\App, your source file location is D:\App\Res\Source.txt and your target location is D:\App\Res\Test\target.txt then your code snippet will be -
File.Copy(Res\\Source.txt, Res\\Test\\target.txt);

Get value from resx in Class Library

I created an App_GlobalResources folder and added relevant resx files for the appropriate country in an ASP Net site.
I then add a key and value to the file. All displays on the site as required.
I am now trying to retrieve the value from this resx file from a class library, mainly using the below code
ResourceManager lang = new ResourceManager("Resource.en-AU", Assembly.Load("App_GlobalResources"));
string value = lang.GetString(Key);
return value;
but the code crashes (second line) with the error
Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resource.en-AU.resources" was correctly embedded or linked into assembly "App_GlobalResources.ufgcy-ty" at compile time, or that all the satellite assemblies required are loadable and fully signed.
So i then tried
ResourceManager myManager = new ResourceManager(typeof("NOTHING AVAILABLE HERE")); // Seems like its expecting a resource file but since its a class library i cant do this
string myString = myManager.GetString("StringKey");
This leads me to believe that i need another way to retrieve the value from a country resx file in a Class Library but not seeing any examples of how to do this OR i need to move the existing resource files from the website to the Class Library and then copy over to the site everytime i make a change but i dont know if this is the correct approach?
Try like this:
ResourceManager resourceManager =
new ResourceManager("Resources.xxx", Assembly.Load("App_GlobalResources"));
string myString = resourceManager.GetString("StringKey");
where xxx is name of your resource file, without .resx extension and without culture name (in your case, name should be Resource (without .en-au).
ResourceManager will try to load specified resource file for current culture, depending on CurrentUICulture. If your server is set to English (Australian) language/regional settings/Culture, it will try to load Resource.en-au if your culture is set to, let's say, Romanian, it will try to find Resource.ro. If such file isn't found, it will fallback to default one, the one without culture name.
Alternatively, you can load resources like this:
[Resources namespace].[Resource file name].ResourceManager.GetString("StringKey")
Resources namespace is Namespace of your resource file (default Resources, but you can see real namespace in your resource file's .Designer.cs file) and resource file name is filename of .resx file, without extension or culture.
So, you can try like this:
string myString = Resources.Resource.ResourceManager.GetString("StringKey");

Get file from RESW resource

I have encountered a problem with retrieving a file for RESW file.
My project structure:
Levels
Level1.xml
Level2.xml
Level3.xml
Resources
LevelResources.resw
And files from "Levels" directory are added to "LevelResources.resw".
My try to retrieve content of those files:
var resourcesLoader = new ResourceLoader("LevelResources");
var item = resourcesLoader.GetString("Level1");
But the value of "item" is"
..\levels\level1.xml;System.String, System.Runtime,
Version=4.0.10.0, Culture=neutral,
PublicKeyToken=b03g5f6f12d40a6a;windows-1250
Why? This is not exactly what I expected (content of the file). How to retrieve content than?
The ResourceLoader class provides simplified access to app resources such as app UI strings.
When you dragging a file to the resource file, the data was stored as a file reference rather than the content, that’s why you will get result “level1.xml;System.String...”.
As a recommend way, I suggest you putting the file name as the key and the file content as the value in the *resw file so that you can use the ResourceLoader to get the content easily.

Can't read a resource file

Here's my code when retrieving the resource file.
ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager("Resource.resx", #"c:\", null);
resourceValue = resourceManager.GetString("key1");
But I got this exception everytime I run this.
Could not find any resources
appropriate for the specified culture
(or the neutral culture) on disk.
baseName: Resources.resx
locationInfo: null fileName:
Resources.resx.resources
What's wrong in my code?
Steven is right, it's looking for a .ressources file. You need to compile your resx file with resgen : http://msdn.microsoft.com/en-us/library/ccec7sz1(v=vs.71).aspx
Some more info : the resx file is just a file that describes how to create a resources file. When you compile it (is compile the right word ?) with resgen, it takes all images, texts, etc.. and merge them in the real resource file that you can distribute and work with.
I suggest the following read, it may help : http://edndoc.esri.com/arcobjects/9.1/ArcGISDevHelp/DevelopmentEnvs/DotNet/WorkingWithResources.htm
It is looking for a different file, as mentioned on msdn, and in your exception message.
baseName
Type: System.String
The root name of the resources. For example, the root name for the
resource file named
"MyResource.en-US.resources" is
"MyResource".
Your file should be named "Resource.resources", when passing "Resource" as baseName to the method. When you want custom resources for different cultures, you should name them like "Resource.en-US.resources" where the "en-US" part is replaced by the desired ISO culture name.

Categories