C # Get the User Name of the current domain

Source: Internet
Author: User

C # modifying the domain account password

Directoryentry entry1 = new directoryentry ("LDAP: //" + "Domain Name", "User Name", "password ");
Directorysearcher searcher1 = new directorysearcher (entry1 );
Searcher1.filter = "(samaccountname =" + "username" + ")";
Searchresult result1 = searcher1.findone ();

"Domain Name" format (Dc = Baidu, Dc = com)

 

C # Get the User Name of the current domain

Ensure your project has referenced DLL "system. Management", then CSHARP codes as below:
Using system. diagnostics;
Using system. Management;
Namespace workrecord
{
Class currentuser
{
Public static string getcurrentuser ()
{
String currentuser = NULL;
Foreach (PROCESS p in process. getprocesses ())
{
Currentuser = getprocessusername (P. ID );
If (currentuser! = "System" & currentuser! = NULL & currentuser! = String. Empty)
Break;
}
Return currentuser;
}
Private Static string getprocessusername (int pid)
{
String theuser = NULL;
Selectquery query1 = new selectquery ("select * From win32_process where processid =" + PID );
Managementobjectsearcher searcher1 = new managementobjectsearcher (query1 );
Try
{
Foreach (managementobject disk in searcher1.get ())
{
Managementbaseobject inpar = NULL;
Managementbaseobject outpar = NULL;
Inpar = disk. getmethodparameters ("getowner ");
Outpar = disk. invokemethod ("getowner", inpar, null );
Theuser = outpar ["user"]. tostring ();
Break;
}
}
Catch
{
Theuser = "system ";
}
Return theuser;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.