By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

Windows User and Domain names

P: n/a
Hi there,
When i use:
WindowsIdentity.GetCurrent().Name.ToString();
I get a concatenated domain name and user name in the format :
DOMAIN\USER_NAME.

Are there functions in C# to get the domain name and user name separately?

Cheers
Zest
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,
To get the user name separately you can import the dll file Advapi32.dll.
See the following code,
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{

class Class1
{
[STAThread]
[DllImport("Advapi32.dll", EntryPoint="GetUserName", ExactSpelling=false,
SetLastError=true)]
static extern bool GetUserName(
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize );
static void Main(string[] args)
{
byte[] str=new byte[256];
Int32[] len=new Int32[1];
len[0]=256;
GetUserName(str,len);
Console.WriteLine(System.Text.Encoding.ASCII.GetSt ring(str));
Console.ReadLine();
}
}
}

Regards, Daya PSP India

"Zest4Csharp" wrote:
Hi there,
When i use:
WindowsIdentity.GetCurrent().Name.ToString();
I get a concatenated domain name and user name in the format :
DOMAIN\USER_NAME.

Are there functions in C# to get the domain name and user name separately?

Cheers
Zest

Nov 17 '05 #2

P: n/a

"Zest4Csharp" <Ze*********@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...
Hi there,
When i use:
WindowsIdentity.GetCurrent().Name.ToString();
I get a concatenated domain name and user name in the format :
DOMAIN\USER_NAME.

Are there functions in C# to get the domain name and user name separately?

Cheers
Zest


No, but it's easy to split the string into a domain and user part...

string[] ar = ident.Split('\\');
string domain = ar[0];
string user = ar[1];

Willy.
Nov 17 '05 #3

P: n/a
Thanks for that Willy and have a good day.

Zest
=====

"Willy Denoyette [MVP]" wrote:

"Zest4Csharp" <Ze*********@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...
Hi there,
When i use:
WindowsIdentity.GetCurrent().Name.ToString();
I get a concatenated domain name and user name in the format :
DOMAIN\USER_NAME.

Are there functions in C# to get the domain name and user name separately?

Cheers
Zest


No, but it's easy to split the string into a domain and user part...

string[] ar = ident.Split('\\');
string domain = ar[0];
string user = ar[1];

Willy.


Nov 17 '05 #4

P: n/a
>To get the user name separately you can import the dll file Advapi32.dll.

Or just use Environment.UserName instead ! ;-)

Marc
================================================== ==============
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.