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

Lou's World Inc 66 Reputation points
2022-03-31T07:41:58.377+00:00

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.

Environment.UserName
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
Environment.GetFolderPath(Environment.SpecialFolder.Desktop))

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,981 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,604 questions
C#
C#
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.
11,002 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,536 Reputation points
    2022-03-31T10:00:11.307+00:00

    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 (SqlWork.com) 66,226 Reputation points
    2022-03-31T15:21:50.037+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.