?
Solved

Get NT Userid in C# without Domain

Posted on 2008-11-11
4
Medium Priority
?
1,259 Views
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?
0
Comment
Question by:SasfinSD
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 22929108
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
 

Author Comment

by:SasfinSD
ID: 22929151
Nope. I get a warning - Too many characters in character literal
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 22929190
SasfinSD,

Should be \\ of course for c#:

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

TimCottee
0
 

Author Closing Comment

by:SasfinSD
ID: 31515460
Thanks for the help Tim - This did the job :-)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

829 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