Windows User and Domain names

G

Guest

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
 
G

Guest

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.GetString(str));
Console.ReadLine();
}
}
}

Regards, Daya PSP India
 
W

Willy Denoyette [MVP]

Zest4Csharp said:
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.
 
G

Guest

Thanks for that Willy and have a good day.

Zest
=====

Willy Denoyette said:
Zest4Csharp said:
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.
 
M

Marc Scheuner [MVP ADSI]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top