Faster way to find out if a user exists on a system?

前端 未结 4 1187
陌清茗
陌清茗 2020-12-08 23:50

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;
Directo         


        
4条回答
  •  温柔的废话
    2020-12-09 00:43

    This should do it (when you can't use System.DirectoryServices.AccountManagement):

    static bool userExists(string sUser)
    {
        using (var oUser = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + sUser + ",user")) 
        {
             return (oUser != null);
        }
    }
    

提交回复
热议问题