473,416 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Windows User and Domain names

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
4 3028
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

"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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wayne Gibson | last post by:
Hi all, Please ignore the other post.. The cat jumped on the machine and sent it before I could stop it!! Was wondering if anybody has expericence this problem.. I am writting an application...
3
by: Steven Fox | last post by:
============================================================ About DB2 Administration Tools Environment ============================================================ DB2 administration tools level:...
3
by: mrwoopey | last post by:
Hi, I am using the example "Authenticate against the Active Directory by Using Forms Authentication and Visual Basic .NET": http://support.microsoft.com/default.aspx?scid=KB;EN-US;326340 ...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
10
by: dermot | last post by:
I have wrriten a small windows service application in visual studio ..net 2003 which listens for incoming FTP files. These files would overwrite over time due to duplicate file names. However any...
0
by: Kofa | last post by:
Hi, I cannot create/drop databases from the command line, only from the GUI. Below is a detailed description of my installation and configuration attempts (sorry about the long post). I'm...
4
by: shashank kadge | last post by:
hi all, i am trying to get local admin users and groups on a windows server. here is the C# code that i am using...
0
by: =?Utf-8?B?UmljayBF?= | last post by:
I have an IIS 6 server on Windows 2003 running in an AD domain attempting to enumerate the files on another Windows 2003 server on the same domain. The code and UNC path are sound as it works if I...
1
by: eblackmo | last post by:
I have a test network consisting of four servers running windows 2003 server R2 SP2. I have set up a domain which functioned correctly for about a day and a half until the other servers decided they...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.