|
| | > 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.