C#获取当前域用户名
2012-05-23 11:06
218 查看
c# 修改域账号密码
DirectoryEntry entry1 = new DirectoryEntry("LDAP://" + “域名”, “用户名",“密码”);
DirectorySearcher searcher1 = new DirectorySearcher(entry1);
searcher1.Filter = "(samAccountName=" + “用户名" + ")";
SearchResult result1 = searcher1.FindOne();
"域名" 格式( dc=baidu,dc=com )
C#获取当前域用户名
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;
}
}
DirectoryEntry entry1 = new DirectoryEntry("LDAP://" + “域名”, “用户名",“密码”);
DirectorySearcher searcher1 = new DirectorySearcher(entry1);
searcher1.Filter = "(samAccountName=" + “用户名" + ")";
SearchResult result1 = searcher1.FindOne();
"域名" 格式( dc=baidu,dc=com )
C#获取当前域用户名
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;
}
}
相关文章推荐
- C#实现微信结合百度api获取当前用户地理位置的方法
- C#微信结合百度api获取当前用户的地理位置
- 使用 C# 获取当前用户的SID
- C# 根据注册表获取当前用户的常用目录整理
- SharePoint使用 C# 获取当前用户的SID
- 用C#获取MOSS中当前登录用户的信息
- 续【C#微信结合百度api获取当前用户的地理位置】代码详解
- C#中获取程序当前路径 (转)
- c# 根据当前时间获取,本周,本月,本季度,月初,月末,各个时间段(转但是都是使用过)
- .Net C# WinForm -- 获取TextBox当前焦点所在位置,插入文本,选中文本
- c#获取当前时间是本年的第几周,本月的第几周
- (原)用C#在WinXP和WinCE里获取应用程序当前路径的通用函数
- WPF获取当前用户控件的父级窗体
- C#获取当前程序运行路径的方法集合
- C#获取当前页面的url (转载)
- [转载].NET C#获取当前网页地址信息
- C# 获取当前路径方法
- oracle获取当前用户表、字段等详细信息SQL
- C#中获取当前路径的几种方法
- 总结C#获取当前路径的7种方法