4

Does anyone know how to get the name of the user who's currently signed into Visual Studio from C#?

Basically I want my VS extension to be able to greet the user by name (ie whoever's signed in to the current VS session).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yann Duran
  • 3,842
  • 1
  • 24
  • 24
  • 1
    https://stackoverflow.com/questions/38264088/get-identity-of-logged-in-visual-studio-user-from-extension? – CodeCaster Jul 09 '19 at 08:21
  • @LanceLi-MSFT I haven't started coding that feature yet, so I'll either post an update if the current solution is sufficient, or I'll ask for more help if it doesn't. Of course if I find an even better solution I'll be happy to report it back here. Thanks for the follow-up. – Yann Duran Jul 15 '19 at 14:30
  • @YannDuran Hi friend, any update for this issue? Maybe you can easily check if the code works in a console app since it actually can work in many situation. Just a reminder:) – LoLance Jul 24 '19 at 07:16

1 Answers1

2

AFAIK, there's no API in VS SDK can support this behavior. So you may have to code to read the related value about current account from Registry using Register.CurrentUser.

Check the answer from Hadi and thanks to him, I think it's what you're after. And it works well for VS2015 and VS2017 after my test.

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Can EnvDte be used to find this info? –  Jul 10 '19 at 07:25
  • @dfhwze Haven't found correct or related api, do you mean [Process.UserName](https://learn.microsoft.com/en-us/dotnet/api/envdte80.process2.username?view=visualstudiosdk-2017#EnvDTE80_Process2_UserName) or something like this. I can't get it to work to get account-related user name:( – LoLance Jul 10 '19 at 07:36
  • EnvDte is an interop library for the VS IDE: https://learn.microsoft.com/en-us/dotnet/api/envdte?view=visualstudiosdk-2017. Perhaps it has methods to get the signed in user. –  Jul 10 '19 at 07:38
  • @LanceLi-MSFT thanks for that.I tried searching StackOverflow a few times, and Google several times, but did see that answer. I guess I just didn't have the right combination of search terms. Someone in the extendvs Gitter room suggested the registry, but I had hoped that by 2019 that there'd be a "better" way of getting such a simple thing. I'll give Hadi's code a try. Thanks again! – Yann Duran Jul 10 '19 at 13:55