Geoff Overfield Geoff Overfield - 28 days ago 14
C# Question

How can I get a pc's user name?

I'm trying the get the user's name [ex) admin, John, Lucy, etc.] so I can store it to a string and create a file path to save files in my application. I have absolutely no idea how to do this. I tried using the

Microsoft.AspNet.Identity;
namespace but that didn't help me any, as the
UserManager
class item is templated to take a
User
, which is what I'm trying to find in the first place. I'm honestly just walking through the forest at night right now. Can't see and no idea where to go. Any help would be awesome!! Thanks

Answer

Try this to get the username of the currently logged on user:

string username = Environment.UserName;

Microsoft Docs

This is in the system namespace, so there's no need to use Windows Identity.