Look it to this Statement : 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.
精彩评论