Get NT Userid in C# without Domain

Hey guys

How can I get the current NT username but without the Domain name in front?

I've done this, but this gives me domainname\username

Label1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

I know I can do it with String manipulation but i've got different domain names and this will get too tricky.

Any advice?
SasfinSDAsked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
SasfinSD,

Should be \\ of course for c#:

Label1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString().Split(new Char [] {'\\'})[1];

TimCottee
0
 
TimCotteeHead of Software ServicesCommented:
Hello SasfinSD,

Label1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString().Split(new Char [] {'\'})[1];

Should do it regardless of the domain name.

Regards,

TimCottee
0
 
SasfinSDAuthor Commented:
Nope. I get a warning - Too many characters in character literal
0
 
SasfinSDAuthor Commented:
Thanks for the help Tim - This did the job :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.