'How to get current location in Windows Form c#
I wanted to get the location of the user in Windows Forms so I copied a code on the internet to get the current location of the device, but it doesn't work. I've tried multiple other codes but none of them seem to be working.
This is the code:
using System;
using System.Device.Location;
namespace CSharpProject
{
public class Location
{
GeoCoordinateWatcher watcher;
GeoCoordinate coord;
public GeoCoordinate Coordination
{
get
{
return coord;
}
}
public void GetCurrentLocation()
{
watcher = new GeoCoordinateWatcher();
watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));
coord = watcher.Position.Location;
if (coord.IsUnknown == true)
throw new Exception("Unknown Location!");
}
}
}
It always goes to the "Unknown Location!" section.
Solution 1:[1]
The first parameter of the TryStart method must be set to True and make sure that you included System.Device.Location in your usings list.
watcher.TryStart(true, TimeSpan.FromMilliseconds(1000));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


