C# 获取当前域名

How to get the domain name

How to get the domain name

How to get the domain name

How to get the domain name

How to get the domain name

How to get the current domain name?

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
bvinoth@tvsinfotech.com

Galcho[MCSD.NET]
Posts: n/a
# 2: May 15 '06

re: How to get the domain name


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

jeremiah johnson
Posts: n/a
# 3: May 15 '06

re: How to get the domain name


Console.WriteLine(Environment.UserDomainName);

None wrote:[color=blue]
> 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
> bvinoth@tvsinfotech.com
>[/color]
None
Posts: n/a
# 4: May 15 '06

re: How to get the domain name


Hi,

This is giving me the username only not domain name. I want only the
domain name.

Willy Denoyette [MVP]
Posts: n/a
# 5: May 15 '06

re: How to get the domain name



"None" <vinkumrect@gmail.com> wrote in message
news:1147675234.188206.42850@i40g2000cwc.googlegro ups.com...
| 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.


Willy Denoyette [MVP]
Posts: n/a
# 6: May 15 '06

re: How to get the domain name





"Galcho[MCSD.NET]" <galcho@gmail.com> wrote in message
news:1147673857.537214.260590@i40g2000cwc.googlegr oups.com...
| 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.


Galcho[MCSD.NET]
Posts: n/a
# 7: May 15 '06

re: How to get the domain name


You're right, Thanks for advise

I did not take a look at Environment class

regards

Galin Iliev[MCSD.NET]
www.galcho.com

Goran Sliskovic
Posts: n/a
# 8: May 15 '06

re: How to get the domain name



"jeremiah johnson" <naikrovek@gmail.com> wrote in message
news:uqgNlc%23dGHA.3888@TK2MSFTNGP02.phx.gbl...[color=blue]
> Console.WriteLine(Environment.UserDomainName);
>
> None wrote:[color=green]
> > Hi,
> >
> > If anybody knows how to get the domain name(only domain name) of the
> > system pls let me know.
> >[/color][/color]
....

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


Willy Denoyette [MVP]
Posts: n/a
# 9: May 15 '06

re: How to get the domain name



"Goran Sliskovic" <gsliskov@yahoo.com> wrote in message
news:eWG0GDAeGHA.3888@TK2MSFTNGP04.phx.gbl...
|
| "jeremiah johnson" <naikrovek@gmail.com> wrote in message
| news:uqgNlc%23dGHA.3888@TK2MSFTNGP02.phx.gbl...
| > 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值