我正在尝试创建一个应用程序,我可以在其中查看过程,路径,用户和描述,例如我已经制作的任务管理器详细信息,但是我想要一个过滤器来搜索图像路径包含的源代码'例如Chrome或者如果您可以帮助我获取描述,例如任务管理器图像并使用此列过滤。如果你可以帮助我获取进程的用户名而不使用另一种方法就会很好。
谢谢。
这是我的改进。
DataTable dt = null;
private void Form1_Load(object sender, EventArgs e)
{
var wmiQueryString = "SELECT * FROM Win32_Process";
// var wmiQueryString = "SELECT * FROM Win32_ComputerSystem";
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
using (var results = searcher.Get())
{
var query = from p in Process.GetProcesses()
join mo in results.Cast<ManagementObject>()
on p.Id equals (int)(uint)mo["ProcessId"]
select new
{
Process = p.ProcessName,
Path = (string)mo["ExecutablePath"],
CommandLine = (string)mo["CommandLine"],
User = GetProcessOwner(p.Id),
Description = mo["Description"]
};
dt = ConvertToDataTable(query);
dataGridView1.DataSource = dt;
}
}
DataTable ConvertToDataTable<TSource>(IEnumerable<TSource> source)
{
var props = typeof(TSource).GetProperties();
var dt = new DataTable();
dt.Columns.AddRange(
props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()
);
source.ToList().ForEach(
i => dt.Rows.Add(props.Select(p => p.GetValue(i, null)).ToArray())
);
Array a = source.ToArray();
for (int i = 0; i < a.Length; i++)
{
var x = a.GetValue(i);
//if (true)
//{
// DataRow r = dt.NewRow();
// r.
//}
a.ToString();
}
return dt;
}
public string GetProcessOwner(int processId)
{
string query = "Select * From Win32_Process Where ProcessID = " + processId; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection processList = searcher.Get(); foreach (ManagementObject obj in processList)
{
string[] argList = new string[] { string.Empty, string.Empty }; int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)); if (returnVal == 0)
{
// return DOMAIN\user
return argList[0];
}
}
return "NO OWNER";
}
答案 0 :(得分:1)
流程描述来自文件描述。你可以像这样抓住它:
private void Form1_Load(object sender, EventArgs e)
{
var wmiQueryString = "SELECT * FROM Win32_Process";
// var wmiQueryString = "SELECT * FROM Win32_ComputerSystem";
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
using (var results = searcher.Get())
{
var query = from p in Process.GetProcesses()
join mo in results.Cast<ManagementObject>()
on p.Id equals (int)(uint)mo["ProcessId"]
select new
{
Process = p.ProcessName,
Path = (string)mo["ExecutablePath"],
CommandLine = (string)mo["CommandLine"],
User = GetProcessOwner(p.Id),
Description = GetDescription((string)mo["ExecutablePath"])
};
dt = ConvertToDataTable(query);
dataGridView1.DataSource = dt;
}
}
string GetDescription(string executablePath)
{
if (!File.Exists(executablePath))
{
return "No Description";
}
return FileVersionInfo.GetVersionInfo(executablePath).FileDescription;
}
您可能需要以管理员身份运行程序才能显示所有流程的详细信息。
您为什么要使用其他方法获取用户名?