How to get the current country name automatically whenever I open the application in Windows Form Application C# visual Studio?

Malex 1 Reputation point
2021-08-12T05:35:03.553+00:00

Hello guys,

I am making a project which is a simple calculator that should display the date, time and the current location country name. I finished all of these except for the country name. I have been researching for almost 5 days now for an answer or any clues on how to find the current country name based on my location.

I found the RegionInfo class which is not exactly what I want because it depends on the language that you put in the Control Panel and I want it to be based on the location.

I also found something called geolocation but didn't know how to use it I have never used it before. When I looked on YouTube I found videos showing you how to get the Longitude and latitude which is also is not what I want. I could not find something on the internet that has a similar question like mine.

Is there anyway to do this can someone help me please?

Best regards,

Malex

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,793 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 80,701 Reputation points
    2021-08-12T06:54:23.327+00:00

    There is CivicAddress Class but it does not work on my PC

    But I get the country with Windows.Devices.Geolocation :

    // Add reference to "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd"  
    // Add reference to "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll"  
    using Windows.Devices.Geolocation;  
    

    Minimal function :

            static async void GetLocationsync()  
            {  
                var accessStatus = await Geolocator.RequestAccessAsync();  
                switch (accessStatus)  
                {  
                    case GeolocationAccessStatus.Allowed:                      
                        Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = 100 };     
                        Geoposition pos = await geolocator.GetGeopositionAsync();  
                        Console.WriteLine("Country : {0}", pos.CivicAddress.Country);  
                        break;  
                    case GeolocationAccessStatus.Denied:  
                        break;  
                    case GeolocationAccessStatus.Unspecified:  
                        break;  
                }  
            }  
    

  2. Castorix31 80,701 Reputation points
    2021-08-13T07:24:05.717+00:00

    Otherwise, you can also check the method with System.Device.Location I posted in this thread C# Label shows Weather Information
    In sLocation variable I get "Town, Country" so, if it works and you want only the country, you can extract it after the comma

    0 comments No comments

  3. Karen Payne MVP 35,006 Reputation points
    2021-08-15T02:38:17.667+00:00

    Why not use

    Require

    using System.Globalization;
    

    Code

    string countryName = RegionInfo.CurrentRegion.DisplayName;
    var regionName = RegionInfo.CurrentRegion.ThreeLetterWindowsRegionName;
    Console.WriteLine($"{countryName}, {regionName}");