RaUI/Source/MyDb/SysFuns/ProcessExt.cs
zilinsoft 3262955f2f ### 2023-11-07更新
------
#### RaUIV4    V4.0.2311.0701
- *.[全新]整合了MyDb、ryControls、MyDb_MySQL等dll文件到RaUI一个项目。
- *.[新增]新增ApkOp类,可以轻松获取APK信息。
- *.[新增]新增JsonExt扩展类,让Json操作更简单。
- *.[新增]新增WebP类,可以支持webp格式的图片。
- *.[改进]ryQuickSQL中的AddField方法改为自动替换已存在的同名值。
- *.[修复]ryQuickSQL中的AddFieldCalc方法无法正常计算的BUG。
2023-11-07 16:37:53 +08:00

83 lines
2.6 KiB
C#

using _SCREEN_CAPTURE;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace ryCommon
{
/// <summary>
/// 进程相关接口
/// </summary>
public static class ProcessExt
{
[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In][MarshalAs(UnmanagedType.U4)] int nSize);
/// <summary>
/// 获取进程路径
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
public static string GetPath(this Process process)
{
if (process == null)
{
return "";
}
return GetPath(process.Id);
}
/// <summary>
/// 获取进程路径
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
public static string GetPath(int pid)
{
var processHandle =WinAPI.Kernel32.OpenProcess(0x0400 | 0x0010, false, pid);
if (processHandle == IntPtr.Zero)
{
return null;
}
const int lengthSb = 4000;
var sb = new StringBuilder(lengthSb);
string result = "";
if (GetModuleFileNameEx(processHandle, IntPtr.Zero, sb, lengthSb) > 0)
{
result = sb.ToString();
}
WinAPI.Kernel32.CloseHandle(processHandle);
return result;
}
/// <summary>
/// 获取命令行
/// </summary>
/// <param name="processs"></param>
/// <returns></returns>
public static string GetCommandLines(this System.Diagnostics.Process processs)
{
if (processs == null) { return ""; }
return GetCommandLines(processs.Id);
}
/// <summary>
/// 获取命令行
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
public static string GetCommandLines(int pid)
{
System.Management.ManagementObjectSearcher commandLineSearcher = new System.Management.ManagementObjectSearcher(
"SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + pid);
string commandLine = "";
foreach (var commandLineObject in commandLineSearcher.Get())
{
commandLine += (string)commandLineObject["CommandLine"];
}
commandLineSearcher.Dispose();
return commandLine;
}
}
}