Get NT Userid in C# without Domain

Posted on 2008-11-11
Last Modified: 2013-12-17
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?
Question by:SasfinSD
    LVL 44

    Expert Comment

    Hello SasfinSD,

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

    Should do it regardless of the domain name.



    Author Comment

    Nope. I get a warning - Too many characters in character literal
    LVL 44

    Accepted Solution


    Should be \\ of course for c#:

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


    Author Closing Comment

    Thanks for the help Tim - This did the job :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now