windows 找出用户是否存在于系统上的更快方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1675813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Faster way to find out if a user exists on a system?
提问by Shaitan00
I have an application that checks to see if a user exists (if not create it) every time it starts. This is done as follows:
我有一个应用程序,每次启动时都会检查用户是否存在(如果没有创建)。这是按如下方式完成的:
bool bUserExists = false;
DirectoryEntry dirEntryLocalMachine =
new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntries dirEntries = dirEntryLocalMachine.Children;
foreach (DirectoryEntry dirEntryUser in dirEntries)
{
bUserExists = dirEntryUser.Name.Equals("UserName",
StringComparison.CurrentCultureIgnoreCase);
if (bUserExists)
break;
}
The problem is on the majority of the systems where it is deployed. This can take 6 - 10 seconds, which is too long ... I need to find a way to reduce this (as much as possible). Is there a betteror fasterway I can use to verify if a user exists on the system or not?
问题出在部署它的大多数系统上。这可能需要 6 - 10 秒,太长了......我需要找到一种方法来减少这种情况(尽可能多)。有没有更好或更快的方法可以用来验证系统上是否存在用户?
I know there are other ways to solve this, like have the other applications sleep for 10 seconds, or have this tool send a message when it is ready, etc... But if I can greatly reduce the time it takes to find the user, it would make my life much easier.
我知道还有其他方法可以解决这个问题,比如让其他应用程序休眠 10 秒,或者让这个工具在准备好时发送消息等等......但是如果我能大大减少找到用户所需的时间,这会让我的生活更轻松。
回答by Michael La Voie
.NET3.5 supports new ADquerying classes under the System.DirectoryServices.AccountManagement
namespace.
.NET3.5 支持命名空间下的新AD查询类System.DirectoryServices.AccountManagement
。
To make use of it, you'll need to add "System.DirectoryServices.AccountManagement" as a reference AND add the using
statement.
要使用它,您需要添加“System.DirectoryServices.AccountManagement”作为参考并添加using
语句。
using System.DirectoryServices.AccountManagement;
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine))
{
UserPrincipal up = UserPrincipal.FindByIdentity(
pc,
IdentityType.SamAccountName,
"UserName");
bool UserExists = (up != null);
}
< .NET 3.5
<.NET 3.5
For versions of .NET prior to 3.5, here is a clean example I found on dotnet-snippets
对于 3.5 之前的 .NET 版本,这是我在dotnet-snippets上找到的一个干净的示例
DirectoryEntry dirEntryLocalMachine =
new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
bool UserExists =
dirEntryLocalMachine.Children.Find(userIdentity, "user") != null;
回答by Beached
You want to use the DirectorySearcher.
您想使用 DirectorySearcher。
Something like this:
像这样的东西:
static bool userexists( string strUserName ) {
string adsPath = string.Format( @"WinNT://{0}", System.Environment.MachineName );
using( DirectoryEntry de = new DirectoryEntry( adsPath ) ) {
try {
return de.Children.Find( strUserName ) != null;
} catch( Exception e ) {
return false;
}
}
}
That should be quicker. Also, you can reduce the properties if all you are doing is checking for existence.
那应该更快。此外,如果您所做的只是检查是否存在,则可以减少属性。
回答by kenny
The following in a command prompt returns 1 if 'username' exists.
如果“用户名”存在,则命令提示符中的以下内容将返回 1。
net user | find "username" /c
净用户 | 找到“用户名”/c
回答by mhu
This should do it (when you can't use System.DirectoryServices.AccountManagement):
这应该这样做(当您不能使用 System.DirectoryServices.AccountManagement 时):
static bool userExists(string sUser)
{
using (var oUser = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + sUser + ",user"))
{
return (oUser != null);
}
}