question

Malex-8138 avatar image
0 Votes"
Malex-8138 asked Malex-8138 commented

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered Malex-8138 commented

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


· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried it but it gave me an error. It says the Geolocator does not contain a definition for "RequestAccessAsync".

Also another error says: "The name "GeolocationAccessStatus" does not exist in the current context."

I already added the package.manifest, the Windows.winmd and System.Runtime.WindowsRuntime.dll.

What should I do to fix these errors can you please tell me?

Best regard,

Malek

0 Votes 0 ·

Have you created a C# .Net Framework project :
122736-netframework.jpg


then added the references :

122670-references.jpg


0 Votes 0 ·
netframework.jpg (72.0 KiB)
references.jpg (81.8 KiB)

Yes I created it the same way you did by creating new project and then clicking on the windows Form App and I added the references. Still, it gives me the same errors. Do you think the problem is in the place where I inserted the function that you sent above? I tried adding new reference called Windows.Devices but when I added it I got a different error that is related to the "Geolocator.RequestAccessAsync()" It says that

There is already a reference for the Geolocator.RequestAccessAsync() in the Windows.Devices reference and in the Windows reference. The error was a bit long. After that I removed the windows.Devices and I got back to the first error. 122811-references-for-visual-studio-v2.png



0 Votes 0 ·

Are you sure you added using at beginning ?

 using Windows.Devices.Geolocation;

then the function inside public partial class Form1 : Form
then a button with a click event to test the function...



0 Votes 0 ·

Hello Castorix31,

Yes I am very sure that I using it I will send a screen shot.



122926-calculator-references.png


The picture below will show you an error that is similar to the one that I talked about before. If you want me to send you my whole code to make it easier for you just let me know because I really want to know how to solve this error and learn from it for next time. Thank you for trying to help me I appreciate it.

122939-error-2-for-geolocation.png


0 Votes 0 ·

From Google, it seems to be a conflict with versions of Windows.winmd and System.Runtime.WindowsRuntime.dll
cs0012-you-must-add-a-reference-to-assembly-windows-version-255-255-255-255-win


0 Votes 0 ·

I fixed the problem. You were right there was a version conflict I just deleted the Windows.winmd because when I used browse to add it, I add it from a different place that was the problem. Now everything is working properly except on how do I apply it. Can you please tell me how to do it?

First I added a button and whenever I pressed on the button it only displayed the country name in the Output window, I only referenced the function GetLocationsync() inside the button_click function.

But I actually want it to be when I start the application I want it to automatically display Country name in the label_box instead of button. I want the country name to display in the place of label3 as shown in the picture below. Is that possible to do can you please tell me? I already copied the function above and put on my code but I don't know what to do after that.

Thank you so much for helping me out. I have been researching the internet a lot on how to solve this error it really exhausted me I could not find anything that can help me with this. Everyday I spent between 8- 9 hours researching about this error I felt my head gonna explode. Whenever I try solving an error I get another error while solving the first error. When I try to solve the second error I get a third error while still solving the second error. It is kind of funny and frustrating at the same time.


123296-calculator-app-picture.png


0 Votes 0 ·
Show more comments
Castorix31 avatar image
0 Votes"
Castorix31 answered

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




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered soren543-0258 commented

Why not use

Require

 using System.Globalization;

Code

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



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Read the question :

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.

0 Votes 0 ·