2017-09-14 78 views
1

我正在運行Windows 10.當我從開始菜單打開「區域&語言設置」時,我可以選擇「國家或地區」。我正在嘗試在C#程序中獲取此值。在C#中,如何獲取Windows 10中「區域和語言」下選擇的「國家或地區」?

我在丹麥。我曾嘗試將我的國家改爲德國(請參閱screenshot),但我無法讓我的代碼返回德國。重新啓動電腦沒有幫助。

我寫了一些代碼靈感來自this thread

我的代碼看起來像這樣(試圖馬上各種各樣的事情,讓所有我能想到的區域/文化的東西):

private static void Main(string[] args) 
{ 
    Thread.CurrentThread.CurrentCulture.ClearCachedData(); 
    Thread.CurrentThread.CurrentUICulture.ClearCachedData(); 
    var thread = new Thread(() => ((Action) (() => 
    { 
     Console.WriteLine("Current culture: {0}", Thread.CurrentThread.CurrentCulture.Name); 
     Console.WriteLine("Current UI culture: {0}", Thread.CurrentThread.CurrentUICulture.Name); 
     Console.WriteLine("Installed UI culture: {0}", CultureInfo.InstalledUICulture.Name); 
     Console.WriteLine("Current region: {0}", RegionInfo.CurrentRegion.ThreeLetterISORegionName); 
     Console.WriteLine("System default LCID: {0}", GetSystemDefaultLCID()); 
    }))()); 
    thread.Start(); 
    thread.Join(); 
    Console.ReadKey(); 
} 

[DllImport("kernel32.dll")] 
private static extern uint GetSystemDefaultLCID(); 

它輸出:

Current culture: en-DK 
Current UI culture: en-US 
Installed UI culture: en-US 
Current region: DNK 
System default LCID: 1033 

我怎樣才能得到我的程序檢測到我選擇了德國?我需要撥打什麼方法或財產?什麼重新啓動或緩存清除可能是必要的?

+2

[RegionInfo.CurrentRegion](https://msdn.microsoft.com/en-us/library/system.globalization.regioninfo。 currentregion(v = vs.110)的.aspx)。有時間升級您的Google-Fu。 –

+0

區域控制面板[Win32]中存在「管理」選項卡,而不是現代設置。點擊「複製設置」和「更改系統區域設置」。可能可能會解決你的quastion –

+0

我終於在這個線程中找到了我的問題的答案:https://stackoverflow.com/questions/8879259/get-current-location-as-specified-in-region-and-language-in- c-sharp –

回答

2

我找到了答案,我的問題在this thread

我正在使用下面的代碼,由@SanjaySingh在該線程中提出,只是稍作修改。

如果我打電話GetMachineCurrentLocationgeoFriendlyname參數集5,我得到的三個字母的ISO地區代碼我要(爲德國,這是"DEU")。

geoFriendlyname的值可以找到here

public static class RegionAndLanguageHelper 
{ 
    #region Constants 

    private const int GEO_FRIENDLYNAME = 8; 

    #endregion 

    #region Private Enums 

    private enum GeoClass : int 
    { 
     Nation = 16, 
     Region = 14, 
    }; 

    #endregion 

    #region Win32 Declarations 

    [DllImport("kernel32.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)] 
    private static extern int GetUserGeoID(GeoClass geoClass); 

    [DllImport("kernel32.dll")] 
    private static extern int GetUserDefaultLCID(); 

    [DllImport("kernel32.dll")] 
    private static extern int GetGeoInfo(int geoid, int geoType, StringBuilder lpGeoData, int cchData, int langid); 

    #endregion 

    #region Public Methods 

    /// <summary> 
    /// Returns machine current location as specified in Region and Language settings. 
    /// </summary> 
    /// <param name="geoFriendlyname"></param> 
    public static string GetMachineCurrentLocation(int geoFriendlyname) 
    { 
     int geoId = GetUserGeoID(GeoClass.Nation); ; 
     int lcid = GetUserDefaultLCID(); 
     StringBuilder locationBuffer = new StringBuilder(100); 
     GetGeoInfo(geoId, geoFriendlyname, locationBuffer, locationBuffer.Capacity, lcid); 

     return locationBuffer.ToString().Trim(); 
    } 

    #endregion 
} 
+0

你能告訴我你傳遞了什麼'GetMachineCurrentLocation(?)',並且你通過調用'GetMachineCurrentLocation()'方法得到了什麼? –

+0

在我的情況:'var test = RegionAndLanguageHelper.GetMachineCurrentLocation(RegionInfo.CurrentRegion.GeoId);'=>'test'是「」(空) –

0

Read msdn documentation: RegionInfo Properties

var regionInfo = RegionInfo.CurrentRegion; 
var name = regionInfo.Name; 
var englishName = regionInfo.EnglishName; 
var displayName = regionInfo.DisplayName; 

Console.WriteLine("Name: {0}", name); 
Console.WriteLine("EnglishName: {0}", englishName); 
Console.WriteLine("DisplayName: {0}", displayName); 

名稱:DE
中文名:德國
顯示名稱:德國

+0

我試過了,正如我原來的帖子所示。它沒有幫助。當我在「地區和語言設置」中更改我的國家時,CurrentThread.CurrentCulture沒有改變。 –

+0

@ClausAppel你嘗試過'var regionInfo = RegionInfo.CurrentRegion;'?爲我工作。 –

+0

@ClausAppel,來自msdn:*「RegionInfo類不會自動檢測系統設置中的更改,而是[CurrentRegion](https://msdn.microsoft.com/en-us/library/system.globalization.regioninfo。 currentregion(v = vs.110).aspx)屬性在您調用[ClearCachedData](https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.clearcacheddata(v = vs。 110).aspx)方法「*。 – Sinatr

0

嘗試

var geographicRegion = new Windows.Globalization.GeographicRegion(); 
    var code = geographicRegion.CodeTwoLetter; 

var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion; 
+1

這是UWP解決方案。 – Sinatr

相關問題