How to read resourcefile from dll c# - c#

I would like to use resourcefiles to get some text.These resourcefiles will be in dll.It's nothing todo with localization just in case you ask.
I want to have the ability to choose which rexfile to use based on a configsettings.
Sample
MyCompany.RexFiles.dll
RexFileA
RexFileB
RexFileC
My question
Given that in a config file I have a settings that decide which rexfile to use eg CurrentRexfile="RexFileB"
How can I default to right rexFile depending on the configSettings.
Any suggestions

You can use the ResourceManager Class to retrieve resources:
System.Reflection.Assembly myAssembly = this.GetType().Assembly;
string rexFile = ConfigurationManager.AppSettings["CuurentRexfile"];
System.Reflection.Assembly otherAssembly = System.Reflection.Assembly.Load(rexFile);
System.Resources.ResourceManager resManager = new System.Resources.ResourceManager("ResourceNamespace.myResources", otherAssembly);
string test = resManager.GetString("resourceString");
more read here

Related

Getting localized resources from MEF modules

I have a library project (SamplePlugin) that is meant to be used as an MEF module and loaded at runtime. Recently, I decided to support internationalization. To that end, I created Properties/Resource.resx and respective Properties/Resource.xx-yy.resx for each supported locale. I then added string resources with locale specific translations to each resource file. However at runtime, I cannot seem to get the culture-correct resource. Here is the relavent code:
Loading the module:
var di = new DirectoryInfo("Plugins/");
var dlls = di.GetFileSystemInfos("*.dll", SearchOption.AllDirectories);
var catalog = new AggregateCatalog();
foreach (var fi in dlls)
{
var ac = new AssemblyCatalog(Assembly.LoadFile(fi.FullName));
catalog.Catalogs.Add(ac);
}
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
Method inside of MEF module (SamplePlugin):
string GetFrenchString()
{
var resourceManager = new ResourceManager(
"SamplePlugin.Properties.Resources",
System.Reflection.Assembly.GetExecutingAssembly());
var culture = new System.Globalization.CultureInfo("fr-FR");
return (string)resourceManager.GetObject("SampleText", culture);
}
File structure of the compiled application:
app.exe
Plugins/SamplePlugin/SamplePlugin.dll
Plugins/SamplePlugin/zh-CN/SamplePlugin.resources.dll
Plugins/SamplePlugin/de-DE/SamplePlugin.resources.dll
Plugins/SamplePlugin/fr-FR/SamplePlugin.resources.dll
...
When I run GetFrenchString() I get "Sample Text" (from resource.resx) instead of "exemple de texte" (from resource.fr-FR.resx). I actually found similar question but the poster answered his own question and provided very little detail in both his question and answer.
Additional details:
The access modifiers of all my resource files are "public"

Api for working with a classes as OOP?

I'm writing a 3rd party app that needs to read in .cs files and be able to manipulate classes, then ultimately save back to file.
The type of code I am looking at would be something like:
var classManager = new classManager();
var classes = classManager.LoadFromFile(filePath);
var class = classes[0]; // Just illustrating more than 1 class can exist in a file
var prop = new ClassProperty {Type=MyType.GetType() };
prop.AddGet("return x+y < 50");
//stuff like prop.ReadOnly = true;
class.AddProperty(prop);
var method = new ClassMethod {signature="int id, string name"};
method.MethodBody = GetMethodBodyAsString(); //not writing out an entire method body here
class.AddMethod(method);
class.SaveToFile(true); //Format code
Does such a library exist?
The .NET Compiler Platform Roslyn is what you're looking for. It supports parsing and editting cs files. Check out this post for an example

Access strings resources from embedded .resx in dll?

I'm completely new to resource files, but there is a need for me to deploy my application as click-once, and that seems to ignore all of my external files (images, .ini files etc...) - Rather than spend time trying to figure it out, I thought I'd learn how to use Resource files properly.
After searching through SO, I've found much code and I've built my resource file. So far it only contains strings, which I thought would be simpler!? Alas...
So I've got my DLL (ValhallaLib.dll) and these two functions for dealing with resources (these functions are held within a static Helper Class, where all my random but useful functions live):
public static Bitmap getImageByName(string imgName)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resName = asm.GetName().Name + ".Properties.Resources";
var rm = new System.Resources.ResourceManager(resName, asm);
return (Bitmap)rm.GetObject(imgName);
}
public static string getStringByName(string var)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resName = asm.GetName().Name + ".Properties.Resources";
var rm = new ResourceManager(resName, asm);
return rm.GetString(var);
}
And I'm attempting to call them with a simple:
CHelpers.getStringByName("db_host_address");
Now, other than getting a MissingManifestException... My problem is, that I have no idea (and can't seem to find a straight answer!!) what the resName should be. My resources file is called: StringResource.resx - Yet, its not a case of saying Assembly.ValhallaLib.StringResource.
Can anyone offer some guidance, please?
Update
I've tried global::ValhallaLib.StringResource - but that's not really what I'm after either.
Update 2
Solved it. I've managed to get it to work with:
public static string getStringByName(string var)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resName = asm.GetName().Name + ".Properties.Resources";
var rm = new ResourceManager("ValhallaLib.StringResource", asm);
return rm.GetString(var);
}
I don't know why I found that so overly complicated. Possibly because I've had about 2 hrs sleep.
Cheers to those who tried though :)
facepalm I've figured it out. I can access the resources file using
public static string getStringByName(string var)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resName = asm.GetName().Name + ".Properties.Resources";
var rm = new ResourceManager("ValhallaLib.StringResource", asm);
return rm.GetString(var);
}
Ignore me :)
resName is a base name for resources. In your case you can simple use full type name of resource class.
var resName = typeof(StringResource).FullName;

Null return from C# application settings file

I am attempting to use this code:
String MainDB = ConfigurationManager.AppSettings["MainDB"];
MessageBox.Show(MainDB);
String MailInfo = ConfigurationManager.AppSettings["MailInfo"];
MessageBox.Show(MailInfo);
String HousingIndexLocation = ConfigurationManager.AppSettings["HousingIndex"];
MessageBox.Show(HousingIndexLocation);
to access values generated by this screen:
Everytime the values are returned null.
what am I missing in my implementation of these settings?
Try
String MainDB = Properties.Settings.Default.MainDB;
MessageBox.Show(MainDB);
String MailInfo = Properties.Settings.Default.MailInfo;
MessageBox.Show(MailInfo);
String HousingIndexLocation = Properties.Settings.Default.HousingIndex;
MessageBox.Show(HousingIndexLocation);
You're getting an error because ConfigurationManager is not the proper way to access properties stored in those files. Take a look at:
Using Settings in C#
Long story short, you access the settings in the Settings file using the Properties namespace:
Properties.Settings.Default.MainDB;
// And so on...

Get all the supported Cultures from a satellite assembly

I am using a satellite assembly to hold all the localization resources in a C# application.
What I need to do is create a menu in the GUI with all the available languages that exists for the application. Is there any way to get information dynamically?
This function returns an array of all the installed cultures in the App_GlobalResources folder - change search path according to your needs.
For the invariant culture it returns "auto".
public static string[] GetInstalledCultures()
{
List<string> cultures = new List<string>();
foreach (string file in Directory.GetFiles(HttpContext.Current.Server.MapPath("/App_GlobalResources"), \\Change folder to search in if needed.
"*.resx", SearchOption.TopDirectoryOnly))
{
string name = file.Split('\\').Last();
name = name.Split('.')[1];
cultures.Add(name != "resx" ? name : "auto"); \\Change "auto" to something else like "en-US" if needed.
}
return cultures.ToArray();
}
You could also use this one for more functionality getting the full CultureInfo instances:
public static CultureInfo[] GetInstalledCultures()
{
List<CultureInfo> cultures = new List<CultureInfo>();
foreach (string file in Directory.GetFiles(HttpContext.Current.Server.MapPath("/App_GlobalResources"), "*.resx", SearchOption.TopDirectoryOnly))
{
string name = file.Split('\\').Last();
name = name.Split('.')[1];
string culture = name != "resx" ? name : "en-US";
cultures.Add(new CultureInfo(culture));
}
return cultures.ToArray();
}
Each satellite assembly for a specific language is named the same but lies in a sub-folder named after the specific culture e.g. fr or fr-CA.
Maybe you can use this fact and scan the folder hierarchy to build up that menu dynamically.

Categories

Resources