代码之家  ›  专栏  ›  技术社区  ›  Claus Appel

在C#中,如何在Windows 10的“地区和语言”下选择“国家或地区”?

  •  3
  • Claus Appel  · 技术社区  · 8 年前

    我在丹麦。我试着把我的国家改成德国(参见 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
    

    如何让我的程序检测到我选择了德国?我需要调用什么方法或属性?什么是重新启动或缓存清除可能是必要的?

    4 回复  |  直到 8 年前
        1
  •  5
  •   Sinatr    8 年前

    我在中找到了问题的答案 this thread

    我正在使用下面的代码,正如@SanjaySingh在该线程中提出的,只是稍微修改了一下。

    如果我打电话 GetMachineCurrentLocation geoFriendlyname 5 ,我得到了我想要的三字母ISO地区代码(对于德国,这是 "DEU" ).

    的值 可以找到 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
    }
    
        2
  •  0
  •   MD TAREQ HASSAN    8 年前

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



    显示名称:德国

        3
  •  0
  •   leoinlios    7 年前

    比所选答案短得多,并且不需要DllImports或ClearCachedData:

    var regKeyGeoId = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International\Geo");
    var geoID = (string)regKeyGeoId.GetValue("Nation");
    var allRegions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.ToString()));
    var regionInfo = allRegions.FirstOrDefault(r => r.GeoId == Int32.Parse(geoID));
    Console.WriteLine("EnglishName:" + regionInfo.EnglishName);
    Console.WriteLine("DisplayName:" + regionInfo.DisplayName);
    Console.WriteLine("NativeName:" + regionInfo.NativeName);
    Console.WriteLine("ISOCurrencySymbol:" + regionInfo.ISOCurrencySymbol);
    Console.WriteLine("Name:" + regionInfo.Name);
    

    赞恩应该得到赞扬,因为我只是他职位的延伸: Get Current Location (as specified in Region and Language) in C#

        4
  •  -1
  •   Jack    8 年前

    尝试

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

        var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;
    
    推荐文章