How to get the domain name

N

None

Hi,

If anybody knows how to get the domain name(only domain name) of the
system pls let me know.


Thanks and Regards,
Vinothkumar B
(e-mail address removed)
 
G

Galcho[MCSD.NET]

it is located in registry

in key
HKLM\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon\DefaultDomainName

read this key and you have it

Hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com
 
W

Willy Denoyette [MVP]

| Hi,
|
| This is giving me the username only not domain name. I want only the
| domain name.
|
No, it's returning the domain name not the user name.

Willy.
 
W

Willy Denoyette [MVP]

| it is located in registry
|
| in key
| HKLM\SOFTWARE\Microsoft\Windows
| NT\CurrentVersion\Winlogon\DefaultDomainName
|
| read this key and you have it
|
| Hope this helps
| Galin Iliev[MCSD.NET]
| www.galcho.com
|
Sure, almost all configuration parameters are stored in the registry, but
you better use the appropriate API's to get this kind of info instead of
reading directly from the registry.
The reason is simple, their location is not documented and is not guaranteed
to remain the same across OS versions, and the registry is subject to access
privilege restrictions.

Willy.
 
G

Goran Sliskovic

jeremiah johnson said:
Console.WriteLine(Environment.UserDomainName);
....

However, there are two domain names involved: domain that computer belongs
to and domain current logged user belongs to. Two may not be the same (in
case there are domain trusts between domains). It is not clear what original
poster wanted though.

Regards,
Goran
 
W

Willy Denoyette [MVP]

|
| | > Console.WriteLine(Environment.UserDomainName);
| >
| > None wrote:
| > > Hi,
| > >
| > > If anybody knows how to get the domain name(only domain name) of
the
| > > system pls let me know.
| > >
| ...
|
| However, there are two domain names involved: domain that computer belongs
| to and domain current logged user belongs to. Two may not be the same (in
| case there are domain trusts between domains). It is not clear what
original
| poster wanted though.
|
| Regards,
| Goran
|
|

That's right. To get Domain info it's better to use WMI or ADSI, both are
accessible from .NET through the System.Management and
System.DirectoryServices namespaces.
To get the machine account domain for instance you can query the
'Win32_ComputerSystem' WMI class like this:


SelectQuery query = new SelectQuery("Win32_ComputerSystem");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get()) {
if((bool)mo["partofdomain"] != true)
Console.WriteLine("Workgroup: {0} ",mo["workgroup"]);
else
Console.WriteLine("Domain: {0} ",mo["domain"]);
}
}

To get the interactive logon account domain, you can query the
Win32_LogonSession and it's associated Win32_Account instance.
If all you need is to get the logon account domain from a program running in
the logon session of an iteractive user, just read the
Environment.UserDomainName property. In all other cases, that is,
applications running in non interactive logon sessions, you cannot rely on
the Environment class to get this kind of info, so you will need to use one
of the above.

Willy.
 

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