Domanda

My C#/WF application uses some files, which are different for different languages. These file can not be placed as resources in satellite assemblies. I wish however to put them in the same directories as the satellite assemblies reside, but I need to know actually where the assembly resides (including the situation, when default language embedded in the binary file is used).

For example, when application switches automatically to polish language, I wish to retreive location:

D:\<app folder>\pl-PL\

Is there a way to do so? Please note, that I wish to retreive this information from the assembly and not by guessing the folder location.


With help of Steve B, here's a solution:

string FindSatelliteAssemblyLocation(CultureInfo culture)
{
    if (culture == CultureInfo.InvariantCulture)
        return Path.GetDirectoryName(Application.ExecutablePath);

    try
    {
        Uri location = new Uri(Assembly.GetExecutingAssembly().GetSatelliteAssembly(culture).CodeBase);
        return Path.GetDirectoryName(location.LocalPath);
    }
    catch
    {
        return FindSatelliteAssemblyLocation(culture.Parent);
    }
}
È stato utile?

Soluzione

You may use the current thread UI culture to get the language :

var subfolder = System.Threading.Thread.CurrentUICulture.Name;

Altri suggerimenti

I feel like you could use a combination of System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) and CultureInfo.CurrentUICulture to figure this out.

For example, if your assembly is located in C:\Program Files\MyCompany\App\ and the Current UI Culture is en_US, you could combine the two:

string exeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string culture = CultureInfo.CurrentUICulture.Name;

string langDir = System.IO.Path.Combine(exeDir, culture);

To produce "C:\Program Files\MyCompany\App\en_US"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top