How can I convert from a SID to an account name in C#

后端 未结 10 1031
囚心锁ツ
囚心锁ツ 2020-12-01 02:56

I have a C# application that scans a directory and gathers some information. I would like to display the account name for each file. I can do this on the local system by g

10条回答
  •  旧巷少年郎
    2020-12-01 03:12

    In C#, get the user SID and assign it to a string variable through:

    string strUser = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
    

    You will need to use string because the ability to resolve to the UserName supports string. In other words, using var varUser will result in a namespace error.

    string strUserName = new System.Security.Principal.SecurityIdentifier(strUser).Translate(typeof(System.Security.Principal.NTAccount)).ToString();
    

提交回复
热议问题