从完全限定的Active Directory域名获取NETBIOS域名有时是一项繁琐的工作.我在这里找到了一个好的答案.
在具有多个林的环境中,如果PC不在您正在查询的林中,则此方法将不起作用.这是因为LDAP://RootDSE
将查询计算机域的信息.
有人可能会问:为什么这么复杂?只需在检索到的第一个点之前使用该名称:
ActiveDirectory.Domain.GetComputerDomain().Name;
Run Code Online (Sandbox Code Playgroud)
或者只是获取用户的域名:
Environment.GetEnvironmentVariable("USERDOMAIN");
Run Code Online (Sandbox Code Playgroud)
要么
Environment.UserDomainName;
Run Code Online (Sandbox Code Playgroud)
但是NETBIOS域名可能完全不同,您或您的计算机可能位于不同的域或林中!所以这种方法只能在简单的环境中使用.
DJ KRAZE的解决方案只需要一个小的修改就可以进行跨域查询.这假设了一种信任关系!
private string GetNetbiosDomainName(string dnsDomainName)
{
string netbiosDomainName = string.Empty;
DirectoryEntry rootDSE = new DirectoryEntry(string.Format("LDAP://{0}/RootDSE",dnsDomainName));
string configurationNamingContext = rootDSE.Properties["configurationNamingContext"][0].ToString();
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://cn=Partitions," + configurationNamingContext);
DirectorySearcher searcher = new DirectorySearcher(searchRoot);
searcher.SearchScope = SearchScope.OneLevel;
searcher.PropertiesToLoad.Add("netbiosname");
searcher.Filter = string.Format("(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))", dnsDomainName);
SearchResult result = searcher.FindOne();
if (result != null)
{
netbiosDomainName = result.Properties["netbiosname"][0].ToString();
}
return netbiosDomainName;
}
Run Code Online (Sandbox Code Playgroud) 在winforms应用程序中使用C#和.Net 4.0:是否可以将UAC屏蔽添加到按钮并保留按钮背景图像?怎么样?
这就是我现在正在使用的,但它删除了图像......
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
public static void UACToButton(Button button, bool add)
{
const Int32 BCM_SETSHIELD = 0x160C;
if (add)
{
// The button must have the flat style
button.FlatStyle = FlatStyle.System;
if (button.Text == "")
// and it must have text to display the shield
button.Text = " ";
SendMessage(button.Handle, BCM_SETSHIELD, 0, 1);
}
else
SendMessage(button.Handle, BCM_SETSHIELD, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
有没有办法直接使用Win32Native命名空间?
我想直接调用该方法:
Win32Native.SendMessageTimeout(new IntPtr(0xffff), 0x1a, IntPtr.Zero, "Environment", 0, 0x3e8, IntPtr.Zero) == IntPtr.Zero;
Run Code Online (Sandbox Code Playgroud)
而不是声明它:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult);
Run Code Online (Sandbox Code Playgroud)
有这样的机会吗?