How can I get UserProfile name in C# code on Azure Windows server

Lou's World Inc 41 Reputation points

Within a C# app I need to get the current user name for an Azure Windows VM (2019). I need the path for the current user, like - c:\Users\my_user_name\Desktop.
Something similar to %userprofile%

The following works in development on a Windows 10 machine but not on an Azure Windows Virtual Machine.


Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,338 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
3,775 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,488 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 24,676 Reputation points

    I assume this is a web application question??? It seems to work in development because the development machine is the client and the server. Once deployed the users do not login and run the web application from the server.

1 additional answer

Sort by: Most helpful
  1. Bruce ( 48,876 Reputation points

    web applications are normally run as service or hosted by IIS which is a service. Applications running as a service do not load a user profile, so you can not get user profile properties.

    if you Remote Desktop to the server and run the app, it will you your login profile.

    note: as suggested, the profile is the account running the application.