網(wǎng)站首頁 編程語言 正文
一、概述
一個Process組件提供了在計算機運行進程的訪問權限。 進程,在最簡單的術語中,是正在運行的應用。提供對本地和遠程進程的訪問權限并使你能夠啟動和停止本地系統(tǒng)進程。
屬性
- Id??? 獲取關聯(lián)進程的唯一標識符。
- ProcessName??? 獲取該進程的名稱。
- MachineName??? 獲取關聯(lián)進程正在其上運行的計算機的名稱。
- SessionId??? 獲取關聯(lián)進程的終端服務會話標識符。
- StandardError??? 獲取用于讀取應用程序錯誤輸出的流。
- StandardInput??? 獲取用于寫入應用程序輸入的流。
- StandardOutput??? 獲取用于讀取應用程序文本輸出的流。
- StartInfo??? 獲取或設置要傳遞給 Process 的 Start() 方法的屬性。
- StartTime??? 獲取關聯(lián)進程啟動的時間。
- Threads??? 獲取在關聯(lián)進程中運行的一組線程。
- Handle??? 獲取關聯(lián)進程的本機句柄。
- MainWindowHandle??? 獲取關聯(lián)進程主窗口的窗口句柄。
- MainWindowTitle??? 獲取進程的主窗口標題。???
- MainModule??? 獲取關聯(lián)進程的主模塊。
- Modules??? 獲取已由關聯(lián)進程加載的模塊。
方法
- GetCurrentProcess()??? 獲取新的 Process 組件并將其與當前活動的進程關聯(lián)。
- GetProcessById(Int32)??? 返回新的 Process 組件(給定本地計算機上某個進程的標識符)。
- GetProcesses()??? 為本地計算機上的每個進程資源創(chuàng)建一個新的 Process 組件。
- GetProcessesByName(String)??? 創(chuàng)建新的 Process 組件的數(shù)組,并將它們與本地計算機上共享指定的進程名稱的所有進程資源關聯(lián)。
- Kill()??? 立即停止關聯(lián)的進程。
- Start()??? 啟動(或重用)此 Process 組件的 StartInfo 屬性指定的進程資源,并將其與該組件關聯(lián)。
- WaitForExit(Int32)???????? 指示 Process 組件在指定的毫秒數(shù)內等待關聯(lián)進程退出。
二、使用方法
1、建立和銷毀進程 System.Diagnosties
System.Diagnostics.Process p=Process.Start("notepad.exe","File.txt");
Thread.Sleep(2000);
p.Kill();
2、后臺運行進程:
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.Arguments = @"/c copy c:\1.txt lpt1";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;//默認為true, 使用外殼程序啟動進程。否則,直接從可執(zhí)行文件創(chuàng)建進程。
Process p = Process.Start(psi);
if (!p.WaitForExit(1000 * 7))//啟動外部程序,等待其退出
{
p.Kill();
}
p.Close();
或者
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");//FileName
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;//將此屬性設置為false,就能重定向進程。。。
psi.UseShellExecute = false;//
Process p = Process.Start(psi);
StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;
sw.WriteLine(@"/c copy c:\1.txt lpt1");
sw.Close();
Console.Write(sr.ReadToEnd());
sr.Close();
p.Close();
3、啟動外部程序
System.Diagnostics.Process.Start("explorer.exe", @"c:/"); //用資源管理器打開C:/
System.Diagnostics.Process.Start("rundll32.exe", @"shell32.dll,Control_RunDLL appwiz.cpl,,0");//打開“添加或刪除程序”的面板:
4、列舉計算機中運行中的進程
var pList = Process.GetProcesses().OrderBy(x => x.Id).Take(10);
foreach (var p in pList)
{
Console.WriteLine(string.Format("ProcessID is {0} \t ProcessName is {1}", p.Id, p.ProcessName));
}
5、獲取進程中的多個模塊
var mList = Process.GetCurrentProcess().Modules;
foreach (ProcessModule m in mList)
{
Console.WriteLine(string.Format("ModuleName is {0} \t ModuleURL is {1} \t ModuleVersion is {2}", m.ModuleName, m.FileName, m.FileVersionInfo.FileVersion));
}
原文鏈接:https://www.cnblogs.com/springsnow/p/11268396.html
相關推薦
- 2022-11-30 Go語言k8s?kubernetes使用leader?election實現(xiàn)選舉_Golang
- 2022-06-27 nginx中配置使用proxy?protocol協(xié)議的全過程_nginx
- 2022-04-08 iOS開發(fā)實現(xiàn)簡單計算器功能_IOS
- 2022-08-24 K8S之StatefulSet有狀態(tài)服務詳解_云其它
- 2022-12-22 C語言中pow函數(shù)使用方法、注意事項以及常見報錯原因_C 語言
- 2022-12-27 golang中日期操作之日期格式化及日期轉換_Golang
- 2023-02-12 JetpackCompose?Scaffold組件使用教程_Android
- 2023-02-14 React?Hydrate原理源碼解析_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支