当前位置: 首页 > 编程语言 > C#

在C#中获取当前位置(指定地区和语言)分享

时间:2023-04-11 11:42:46 C#

C#获取当前位置(指定地区和语言)如何获取操作系统指定地区和语言的机器当前位置?我试过从RegionInfo类中获取它,但它返回区域格式下拉列表中指定的区域和位置。只是为了澄清我的意思,如果你从机器的控制面板打开区域和语言,我想读取在位置选项卡中指定的位置。RegionInfo为我提供了在格式下拉列表的格式选项卡中指定的值。经过大量谷歌搜索后,我终于得到了答案。以下两个链接帮助我获得了当前机器位置-http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/6dfaa142-c588-4cb0-b044-fa1e8138b299http://www.siao2.com/2007/02/21/1733999.aspx如果有人对最终代码感兴趣,我制作了以下实用程序类-publicstaticclassRegionAndLanguageHelper{#regionConstantsprivateconstintGEO_FRIENDLYNAME=8;#endregion#regionPrivateEnumsprivateenumGeoClass:int{Nation=16,Region=14,};#endregion#regionWin32声明[DllImport("kernel32.dll",ExactSpelling=true,CallingConvention=CallingConvention.StdCall,SetLastError=true)]privatestaticexternintGetUserGeoID(GeoClassgeoClass);[DllImport("kernel32.dll")]privatestaticexternintGetUserDefaultLCID();[DllImport("kernel32.dll")]privatestaticexternintGetGeoInfo(intgeoid,intgeoType,StringBuilderlpGeoData,intcchData,intlangid);#endregion#region公共方法//////返回区域和语言设置中指定的机器当前位置秒。///publicstaticstringGetMachineCurrentLocation(){intgeoId=GetUserGeoID(GeoClass.Nation);;intlcid=GetUserDefaultLCID();StringBuilderlocationBuffer=newStringBuilder(100);GetGeoInfo(geoId,GEO_FRIENDLYNAME,locationBuffer,locationBuffer,lcid);返回locationBuffer.ToString().Trim();}#endregion}是的..但更容易:CultureInfoinfo=CultureInfo.CurrentCulture;你能试试吗?RegionInfo.CurrentRegion.DisplayName;这是否为您提供了所需的位置名称基于“控制面板>区域>属性位置”,您可以获得RegionInfo试试这个——以上是C#学习教程:获取C#中的当前位置(指定地区和语言)所有内容分享,如果对大家有用,需要详细了解C#学习教程,希望大家多多指教多加注意——varregKeyGeoId=Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"ControlPanelInternationalGeo");vargeoID=(string)regKeyGeoId.GetValue("国家");varallRegions=CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x=>newRegionInfo(x.ToString()));varregionInfo=allRegions.FirstOrDefault(r=>r.GeoId==Int32.Parse(geoID));点击右侧联系管理员删除。如需转载请注明出处: