Resource file code not generated - c#

I have my default resource file Resources.resx for which visual studio nicely generates a designer.cs class, but when I try to create Resources.de-DE.resx, it does not generate.
I checked all the properties for both files are same.
It does generate for Resources1.resx, but not for Resources.de-DE.resx or Resources.en-US.resx.

hello #TRS you may be confused about that there is a Designer.cs present For Resources.resx and there should be someDesigner.cs for Resources.de-DE.resx but this is not the case because designer file would be same for all the resource files. and also the property that is created in Designer.cs is also common that means you will use this property for every conversion so the difference that you can make is on the basis of ResourceCulture.

Yes, Designer resx file remains empty.
you should follow the below steps, it will work 100%.
1) Create Resource.resx file under any folder
2) set CustomTools = PublicResXFileCodeGenerator and
Build Action = Embedded Resource in Resource.resx file property.
3) Now create the different language files like below,
Resources.en-US.resx
Resources.nl-NL.resx etc
4)If MVC, in your MVC Model refer this namespace as project.folder
5) change the one of the property as below (example)
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}"), Display(Name = "dtMovinDate",ResourceType = typeof(Resources.Resource))]
public DateTime dtMovinDate { get; set; }
6)
In config file add this under System.web
<globalization enableClientBasedCulture="true" culture="en-US" uiCulture="en-US" />
7) rebuild the project and test it.
It will work. The real reason of designer.cs file empty is resource designer.cs will refer these files based on your web.config file.

Interestingly, I had similar situation today. I will share the scenario and solution here because I think it's related to your question.
I wanted to have a resource file for Arabic culture only, SharedResources.ar.resx, and I don't have another resource called SharedResources.resx.
The solution was to create the resource file with single [dot] in it, then rename it.
For example, in my case, I created the file with this name, SharedResources-ar.resx. Then double click and change its access modifier to public using the editor, like this:
This will generate .cs file with the correct static properties.
After that, rename the file to the correct format. SharedResources.ar.resx. Otherwise it will not be generated!
I don't know if it's a good solution, but solve my problem and hope to solve someone's else too.

Related

absolute sound path to work in other machines

I am using the following code:
SoundPlayer soundPlay = new SoundPlayer(#"C:\more\more\Assets\Sounds\menuHoover.mp3");
soundPlay.Play();
when clicking in a button to reproduce a sound, but I have to use the absolute path to works, since I am not the only one doing working on this project, I have to use a poth comon to others too. How can I use the relative path to make this work?
I have tried diferent paths, but none seems to work. Only the absolute one.
You may include the file as Embedded Resource into your executable. Then there is no need to distrubute the file separately.
To access the sound resource, use Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("<Namespace>.menuHoover.mp3"); and initialize the SoundPlayer as new SoundPlayer(stream);
What to put in <Namespace>? Have a look in the project properties at "Default namespace". Suppose this is A.B.C. This would be the Namespace if you add the mp3 file at the root directory of the project. If you add it to a sub-directory, say X\Y\Z, the namespace would be A.B.C.X.Y.Z (special rules apply if the directory name contains characters not allowed in an identifier). See also Name of embedded resource
You have several options one is to add the location of the assets to a configuration file such as app.config
For example you could add to your app.config file:
<configuration>
<appSettings>
<add key="AssetPath" value="C:\\more\\more\\Assets\\Sounds\\"/>
</appSettings>
</configuration>
Then in your code you could use something like:
string assetPath = ConfigurationManager.AppSettings["AssetPath"];
SoundPlayer soundPlay = new SoundPlayer(Path.Combine(assetPath,"menuHoover.mp3");
soundPlay.Play();
Another would be to build a path dynamically based on where your application was executed from and then build a relative path from that:
string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
string assetPath = Path.Combine(appDirectory,"Assets","Sounds");
SoundPlayer soundPlay = new SoundPlayer(Path.Combine(assetPath,"menuHoover.mp3");
soundPlay.Play();

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");

How does use XamlReader to load from a Xaml file from within the assembly?

I've found several posts across stackoverflow and the rest of the internet regarding how to load Xaml from a static file: they recommend creating a XmlReader or StreamReader pointing to a file found on the file system, but the .xaml document I would like to read from is going to be compiled with the rest of the assembly, so it won't have a meaningful file Uri. I do not want to copy this document around wherever the assembly goes. Is there a way to read from a .xaml document that has been compiled into the assembly?
I also know that I can simply read from a very long string literal inside the code itself, but I'd rather not do that - the UIElement produced from the Xaml should be easily edited, and I gain this by editing it in a Xaml file.
To illustrate what I'm hoping for, here's an example:
private void LoadUIElementFromCompiledXaml()
{
XmlReader xmlReader = new XmlReader("*Uri for .xaml document within my assembly*");
UIElement elementLoaded = (UIElement)XamlReader.Load(xmlReader);
}
I apologize in advance if the answer is blatantly obvious.
Before you can load Xaml from an assembly as an embedded resource, there is a bit of setup you must do. I'll walk you through an example, then from there you can customize it to suite your needs.
Create folder in your project. Name it XAML.
Add a XAML file to the XAML folder. Lets call it Sample.xaml.
Right-click on Sample.Xaml and choose properties. Set the value for Build Action to "Embedded Resource".
Right-click on the project and choose properties. Take note of the Default namespace value. We will use this as part of the path. For this example lets assume it is "MyNamespace.
Your code to load the Xaml resource would look something like this:
string defaultNamespace = "MyNamespace";
string folderName = "XAML";
string fileName = "Sample.xaml";
string path = String.Format("{0}.{1}.{2}", defaultNamespace, folderName, fileName);
using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
{
object root = XamlReader.Load(stream);
}
As you can see the path to the resource is made up of the default namespace of the project, the folder path to the file, and the file name. If the folder path has multiple levels use dots as folder separator in place of back slashes. For example Xaml\Subfolder would be Xaml.Subfolder.

GetManifestResourceStream returns NULL

This is a C# .NET 4.0 application:
I'm embedding a text file as a resource and then trying to display it in a dialog box:
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyProj.Help.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(result, "MyProj", MessageBoxButtons.OK);
}
}
The solution is MyProjSolution and the executable is MyProj.exe. Help.txt is an embedded resource. However, the stream is null. I've tried MyProjSolution.Help.txt and MyProjSolution.MyProj.Help.txt but nothing seems to work.
You can check that the resources are correctly embedded by using
//From the assembly where this code lives!
this.GetType().Assembly.GetManifestResourceNames()
//or from the entry point to the application - there is a difference!
Assembly.GetExecutingAssembly().GetManifestResourceNames()
when debugging. This will list all the (fully qualified) names of all resources embedded in the assembly your code is written in.
See Assembly.GetManifestResourceNames() on MSDN.
Simply copy the relevant name, and use that instead of whatever you have defined in the variable 'resourceName'.
Notes - the resource name is case sensitive, and if you have incorrectly embedded the resource file, it will not show up in the list returned by the call to GetManifestResourceNames(). Also - make sure you are reading the resource from the correct assembly (if multiple assemblies are used) - it's all too easy to get the resources from the currently executing assembly rather than from a referenced assembly.
EDIT - .NET Core
Please see this SO post for details on how to embed using .NET Core.
Retrieving the manifest info looks to be similar - just use this.GetType().GetTypeInfo().Assembly.GetManifestResourceNames() to get the a manifest from the assembly where the code is executing.
I haven't figured out how to do the equivalent of Assembly.GetExecutingAssembly() in .NET Core yet! if anyone knows - please let me know and I will update this answer.
I had a similar issue check first that the file is included in your project , then go to properties and set the build action of that file to Embedded Resource . this worked for me .
The embedded file's "Build Action" property should be set as "Embedded Resource" to run the line, which is given below, properly:
Stream stream = assembly.GetManifestResourceStream(resourceName)
Right click on the file, click the property and then set "Build Action" property as "Embedded Resource":
Here is the cause of my null value.
http://adrianmejia.com/blog/2011/07/18/cs-getmanifestresourcestream-gotcha/
The GetManifestResourceStream method will always returns NULL if the resource ‘built action‘ property is not set to ‘embedded resource‘
After setting this property with all the needed files assembly.GetManifestResourceStream starts returning the correct stream instead of NULL.
Just a warning.
I could not access my file as an embedded resource even though I specified that it was and even though it had that Build Action property. Wasted a lot of time banging my head. I embedded a csharp code file with .txt appended to its name (xxx.cs.txt). For some reason the GetManifestResourceNames() and GetManifestResourceStream() methods won't see a file with .cs in its name.
I renamed it simply xxx.txt and everything was fine.
Weird.
In my case the problem was that the code looking for the resource was in a different project that the resource itself.
You can only access resources that are in the same project the code is. I thought I could put all my resources in the web page project, but I need images in the mail project too.
Hope this helps someone in the same situation I was.
I find really useful calling Assembly.GetExecutingAssembly().GetManifestResourceNames();.
I had the same problem, thanks to Jay I found it was hyphens in the directory name.
ProjectName.ResourceFolder.Sub-Directory becomes ProjectName.ResourceFolder.Sub_Directory when you reference the resource stream.
A simple and streamlined solution is to have this base class:
public class EmbededResourceReader
{
protected string LoadString(string fileName)
{
return LoadString(fileName, Encoding.UTF8);
}
protected string LoadString(string fileName, Encoding encoding)
{
var assembly = this.GetType().Assembly;
var resourceStream = assembly.GetManifestResourceStream($"{this.GetType().Namespace}.{fileName}");
using (var reader = new StreamReader(resourceStream, encoding))
{
return reader.ReadToEnd();
}
}
}
Then, when you add a resource, you create a reader C# class in the same folder:
where the reader class MyResource.cs is very simple:
public class MyResource : EmbededResourceReader
{
public string LoadString() => LoadString($"{nameof(MyResource)}.txt");
}
So, each resource will have a "shadow" class that knows how to read it properly.
This is how you read the resource in your code:
var text = new MyResource().LoadString();
And as other answers suggested, do not forget to set "Embedded Resource" in the Build Action property of the resource file.
The advantage of this uniform solution is
less hassle with finding correct full name of the resource, especially when placed in nested folders
in case when folder is renamed OR Default Namespace in project settings is changed, the code will NOT break
In case it helps anyone else, Make sure Assembly.GetExecutingAssembly() line is called from same assembly which has embedded resources.
First Unload the project and click on edit the project file.
Inside the project file make sure that the item you are fetching from the assembly is included inside <EmbeddedResource> tag.
Eg:
<ItemGroup>
<EmbeddedResource Include="Template\ForExampleFile.html" />
</ItemGroup>
The files I added into the project were just in Content tag but not in the EmbeddedResource as shown below by default. Hence the stream was returning null.
<ItemGroup>
<Content Include="Template\ForExampleFile.html" />
</ItemGroup>
You need to unload your solution.Then edit project.Afterfind your folder and change like this:
<EmbeddedResource Include="yourpath" />
Although OP got GetManifestResourceStream returning NULL from resources in the same Assembly, some Answers suggested that when Resources are in another Project or Assembly they cannot be retrieved, and are a fair cause of GetManifestResourceStream returning NULL.
This is not true, at least since 2011; as I pointed in some comments elsewhere, Assembly.LoadFrom() or typeof do the trick and as a result you can access resources that are in another project.
I have a moderately complex example here to illustrate; this is my test setup:
Path to another project:
Captured here:
var sharedXMLResource =
"D:\\My Documents\\Consultório Impressos\\DB Pacientes\\Teste\\TestesVariados\\WinFormFramework\\Read_Embedded_XML_File_CS\\bin\\Debug\\Read_Embedded_XML_File_CS.exe";
And on Form1.cs from WinFormFramework I specify with
Namespace.Folder.Resource
like that:
StreamReader reader =
new StreamReader(Assembly.LoadFrom(sharedXMLResource).GetManifestResourceStream("Read_Embedded_XML_File_CS.SharedResources.ContactList.xml") ?? throw new InvalidOperationException());
And the result displayed at textbox:
I spent several hours to get it right; for that, I had to use a lot these at Immediate Window:
Environment.CurrentDirectory
AppDomain.CurrentDomain.BaseDirectory
System.Reflection.Assembly.GetExecutingAssembly().Location
System.Reflection.Assembly.GetAssembly(typeof(WinFormFramework.Program)).Location
Hope it helps someone
I know this is an old question, however, I stumbled through this and with the help of #Jay answer I was able to get this working.
I had to use the full filename to get this to work.
using var stream = assembly.GetManifestResourceStream("Project.Folder.appsettings.json");
You probably need to specify the path to your txt file in the GetManifestResourceStream parameter, or you could try sticking the txt file in the same directory as your executable. Hope that helps!

Can't load a manifest resource with GetManifestResourceStream()

I've created a custom configuration section using XSD. In order to parse the config file that follows this new schema, I load the resource (my .xsd file) with this:
public partial class MonitoringConfiguration
{
public const string ConfigXsd = "MonitoringAPI.Configuration.MonitoringConfiguration.xsd";
public const string ConfigSchema = "urn:MonitoringConfiguration-1.0";
private static XmlSchemaSet xmlSchemaSet;
static MonitoringConfiguration()
{
xmlSchemaSet = new XmlSchemaSet();
Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);
XmlReader schemaReader = XmlReader.Create(xsdStream);
xmlSchemaSet.Add(ConfigSchema, schemaReader);
}
}
By the way my resource is: MonitoringConfiguration.xsd. And the namespace of the other partial class (that represents the code behind of the .xsd file) is MonitoringAPI.Configuration.
The problem is situated here:
Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);
The xsdStream is null, so I guess the resource can't be found! But why?
Thank you
The name of the resource is always:
<Base namespace>.<RelativePathInProject>.<FileName>
So if your resource is located in "Resources/Xsd/", and your default project namespace is "MonitoringAPI.Configuration", the resource name is:
"MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd"
Also make sure the build action for your resource is set to "Embedded Resource"
Easy and correct way to get the actual name of your embedded resource:
string[] resourceNames =
Assembly.GetExecutingAssembly().GetManifestResourceNames();
Then simply check resourceNames array, and you will know for sure what to pass to GetManifestResourceStream method.
In my case,
When you try to access the file via GetManifestResourceStream(). You will get an error due to invalid path of the file, and stream will be null.
Solution:
Right click on the file which you have added in to solution and Click on Properties.
Select the Build Action as Embedded Resource. (Instead of Content - by default)
By default, visual studio does not embed xsd file therefore you must ensure "Build Action" property of xsd file is set to "Embedded Resource" to make it works
just add your resources under form1.resx -->add existing items
double click on the resources you added under Resources folder.go to properties and select "Embedded Resources" instead of none.
Then
try debugging the line:
string[] resourceNames=Assembly.GetExecutingAssembly().GetManifestResourceNames();
check the resources you added are in the array. then copy the resource name exactly from this array and try putting the name on your code..it works fine!!
You can get the Resource Stream by passing the Resource Names which is as follows below...
Get the resource name e.g..
Assembly objAssembly = Assembly.GetExecutingAssembly();
string[] strResourceNames = objAssembly.GetManifestResourceNames();
Pass the Resource Names to ...
Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);
Now you have Stream you can do whatever you want...
In my case, it was something completely different:
My UWP App compiled correctly in Debug and Release configuration but GetManifestResourceStream returned Null only Release configuration.
The issue was, that in the UWP Build Configuration file (and only there) the setting "Compile with .NET Native tool chain" was enabled. After disabling, GetManifestResourceStream worked as expected.
I had an issue where I was embedding a whole heap of .xsd files in many different assemblies; everything was working (GetManifestResourceNames was returning the files I'd expect to see) except for one. The one which wasn't was called:
Something.LA.xsd
I wasn't dealing with specific cultures and the .LA bit at the end of the filename was being picked up by the compiler as this file being for the LA culture - the filename in the manifest was going in as Something.xsd (under culture LA) - hence me not being able to find it (it ended up in a satellite assembly). I dodged the issue by renaming the file - presumably it is possible to explicitly state the culture of a given embedded resource.
Actually, a quick google reveals:
How can I prevent embedded resource file culture being set based on its filename
According to this answer, you've got to do hacky things - so perhaps renaming the file wasn't so bad after all :)

Categories