开发者

Look it to this Statement : System.Security.Principal.WindowsIdentity.GetCurrent().Name;

开发者 https://www.devze.com 2022-12-12 11:31 出处:网络
Look at th开发者_开发百科is statement : messageBox.show( System.Security.Principal.WindowsIdentity.GetCurrent().Name);

Look at th开发者_开发百科is statement :

messageBox.show( System.Security.Principal.WindowsIdentity.GetCurrent().Name);

the output of this statement is :

Roshan\mohdibrahim.tasal

but i want to display me only :

mohdibrhaim.tasal

how can i do this?


You can just split the name on the "\" and retrieve the 2nd item.

e.g.

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1]

Edit: You'll want to make this safe by first checking for the existence of a backslash - if there isn't one, you just want to take the Name as-is.


Why don't you trim returned value until '\' is reached, following code does the trick

WindowsIdentity current = System.Security.Principal.WindowsIdentity.GetCurrent();
if(current!=null)
{
     string name = current.Name;
     string value = name.Substring(name.IndexOf('\\') + 1);
}


Environment.UserName should work without any splitting or additional logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号