如果你不想使用WMI,並寧願做這個土生土長的方式,我寫了基本要求NtQueryInformationProcess()
和派生從返回的信息的命令行的DLL。
它是用C++編寫的,沒有依賴關係,因此它可以在任何Windows系統上工作。
要使用它,只需添加這些進口:
[DllImport("ProcCmdLine32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetProcCmdLine")]
public extern static bool GetProcCmdLine32(uint nProcId, StringBuilder sb, uint dwSizeBuf);
[DllImport("ProcCmdLine64.dll", CharSet = CharSet.Unicode, EntryPoint = "GetProcCmdLine")]
public extern static bool GetProcCmdLine64(uint nProcId, StringBuilder sb, uint dwSizeBuf);
然後調用它像這樣:
public static string GetCommandLineOfProcess(Process proc)
{
// max size of a command line is USHORT/sizeof(WCHAR), so we are going
// just allocate max USHORT for sanity's sake.
var sb = new StringBuilder(0xFFFF);
switch (IntPtr.Size)
{
case 4: GetProcCmdLine32((uint)proc.Id, sb, (uint)sb.Capacity); break;
case 8: GetProcCmdLine64((uint)proc.Id, sb, (uint)sb.Capacity); break;
}
return sb.ToString();
}
的源代碼/ DLL是可用here。
您是否可以控制要重啓的應用程序? – 2010-04-13 22:30:35
是的,我完全控制了我嘗試重新啓動的應用程序代碼 - 它總是會成爲我運行的同一應用程序的另一個實例。這是一個WPF應用程序,如果這有所作爲,但我認爲它不應該。 – 2010-04-13 22:36:25
根據StartInfo上的MSDN文章(http://msdn.microsoft.com/en-us/library/system.diagnostics.process.startinfo.aspx),如果該進程是使用Process啓動的,則StartInfo對象僅包含信息。開始。它還表示在使用GetProcesses *函數時,StartInfo將爲空。 – Corin 2010-04-13 22:37:27