-3

Well, I posted this question earlier but I haven't got the correct answer before it was marked as a duplicate. I think this is a different question, all the methods that I used returns a wrong output.

I wanted to know how to solve this. A sample output is here:

enter image description here

the code:

Imports System.Security.Principal
Imports System.Threading
Imports System.IO
Imports System

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox("1: " & System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString & vbCrLf & _
               "2: " & Environment.UserDomainName & vbCrLf & _
               "3: " & WindowsIdentity.GetCurrent().Name & vbCrLf & _
                "4: " & Thread.CurrentPrincipal.Identity.Name & vbCrLf & _
               "5: " & Environment.UserName & vbCrLf & _
               "6: " & My.User.Name & vbCrLf &
                "7: " & My.Computer.Name)

        ' System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName()
    End Sub

End Class

I don't even know the correct term / name for this Windows username.

halfer
  • 19,824
  • 17
  • 99
  • 186
Codemunkeee
  • 1,585
  • 5
  • 17
  • 29
  • @Blorgbeard user is saying previous was marked duplicate but it isnt. – Baljeetsingh Sucharia Mar 05 '14 at 03:57
  • OP is saying that, but it's not clear that it's not a duplicate. He hasn't said what methods he's tried, or how they failed. – Blorgbeard Mar 05 '14 at 03:58
  • try `echo %username%` in command prompt and see what you get – Sriram Sakthivel Mar 05 '14 at 03:59
  • @blorgbeard right click the image and copy the url, there you can see the methods that I used. I even used the answer from the "duplicate question", but it's not returning the correct answer. – Codemunkeee Mar 05 '14 at 03:59
  • @Sriram I got C:\Users\BESToffshoreSERVICE – Codemunkeee Mar 05 '14 at 04:00
  • Pro tip: if one question was put on hold as a duplicate, asking the same question again only makes it worse. – Joel Coehoorn Mar 05 '14 at 04:00
  • 2
    Also, please don't post screenshots of code; post it as text. Then people can copy+paste it, other people can find it via search, etc. – Blorgbeard Mar 05 '14 at 04:01
  • @JoelCoehoorn Pro tip: I even haven't got the correct answer, how can I even say it's duplicate? I don't even know the source of the problem. at Blorgbeard, I have edited the code.. thanks. :) – Codemunkeee Mar 05 '14 at 04:04
  • @Codemunkeee because now, if nothing else, this is a duplicate of the one you asked the first time. Remember: On hold is not the end of the story. Devote your energy to getting that one re-opened. FWIW: I agree with you that your first question is not a duplicate of the one for which is was put on hold... but this question is a duplicate of your first, and needs to be closed. To that end: try editing your first question to ask for the user's _Display Name_ instead of user name, and make sure to clearly specify you want the logged in user, as opposed to the user running your process. – Joel Coehoorn Mar 05 '14 at 04:06
  • @Joel, you're right. but marking my first post as duplicate will make other people think that it is solved, or whatsoever, which hinders them to click my first post instead. I loved that SO makes it like that, but it's just that I don't think that my question is marked. I hate them. lol – Codemunkeee Mar 05 '14 at 04:08
  • Again: the status for your first question is not final. It can be re-opened. But you'll need to do some work first to clarify how your question is different. – Joel Coehoorn Mar 05 '14 at 04:10
  • @Codemunkeee - the only comment you've made so far is "it is not what I want", but there is no indication of what you actually expect. Try reading your post as third person and see if you can add missing details. (update your original post, not this one) – Alexei Levenkov Mar 05 '14 at 04:11
  • 1
    Actually, he does explicitly say what he expects. Look at the screenshot. He shows what he expects ("the correct ans.") and he shows what he is getting from all of the things he has been told to try (the other red box). Certainly not 100% clear, but its there. – Tim Mar 05 '14 at 04:23

1 Answers1

2

Use System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName

As noted by the replies on this thread, you can use this by adding a reference to System.DirectoryServices.AccountManagement.dll in your project.

grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • 1
    It's not working, DirectoryServices is not a member of system. I have already imported System on my code `Imports System` – Codemunkeee Mar 05 '14 at 04:02
  • 1
    You need to reference System.DirectoryServices.AccountManagement.dll (thanks @Blorgbeard - didn't realize it was split up even further) – Tim Mar 05 '14 at 04:03
  • [MSDN](http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx) says it's in `System.DirectoryServices.AccountManagement.dll` – Blorgbeard Mar 05 '14 at 04:04
  • i get this error PrincipalServerDownException: The server could not be contacted. – Baljeetsingh Sucharia Mar 05 '14 at 04:08
  • 1
    @Codemunkeee following link for sure will help you , and as you can see they are already on Stackoverflow - so duplicate is fine. http://stackoverflow.com/questions/6692767/how-do-i-get-the-ad-display-name-of-the-currently-logged-in-user and http://stackoverflow.com/questions/9845444/how-to-get-a-username-in-active-directory-from-a-display-name-in-c – Baljeetsingh Sucharia Mar 05 '14 at 04:32
  • thank you so much baljjetsingh sucharia.. It indeed helped me :D – Codemunkeee Mar 05 '14 at 05:13