我想在C#中創建一個Windows應用程序,當它運行時除了它自己以外的所有進程都被殺死。除了那些訪問被拒絕的所有進程被拒絕錯誤
我使用以下邏輯:
- 獲取所有的進程並讓他們存儲在列表中。
- 創建一個例外列表(列表中將包含所有允許運行的進程名稱)。
- 將所有正在運行的進程與例外列表進行比較。如果除了提到的任何進程正在運行,則會被殺死。
但也有一些過程,是不允許在這種情況下,即access is denied
殺死添加這些流程例外列表中。
但問題是,此代碼運行並在某些時候屏幕掛起和應用程序關閉。
下面是代碼
Process[] processlist = Process.GetProcesses();
string[] recProc = { "winlogon", "VCSExpress", "media player.vshost","VCSExpress", "taskmgr", "smss", "csrss", "wininit", "csrss", "services",
"lsass", "lsm", "svchost", "winlogon", "WLTRYSVC", "wlanext", "conhost", "BCMWLTRY",
"AvastSvc", "spoolsv", "svchost", "AERTSr64", "btwdins", "NitroPDFDriverService", "NLSSRV32",
"QDLService2kDell", "dwm", "taskhost", "explorer", "RAVCpl64", "AvastUI", "SearchIndexer",
"wmpnetwk", "firefox", "bitlord", "IAStorDataMgrSvc", "LMS", "svchost","media player",
"UNS", "regsvr32", "Skype", "OSPPSVC", "audiodg", "NitroPDF", "chrome", "AAM Updates Notifier",
"mscorsvw", "media player.vshost", "WmiPrvSE", "conhost", "tasklist","regsvr32","RAVCpl64","idle","dwm","conhost"};
List<string> listOfprocess = new List<string>(recProc);
foreach(Process theprocess in processlist)
{
int recProclength = listOfprocess.Count;
int incrementor;
for (incrementor=0; incrementor < processlist.Length;incrementor++)
{
if (theprocess.ProcessName != listOfprocess[incrementor])
{
try {
theprocess.Kill();
}
catch(Win32Exception w){
listOfprocess.Add(theprocess.ProcessName);
incrementor = 0;
break;
}
}
}
}
也許其中一個你殺死的進程比你想象的更重要... –
你正在終止一切Windows依賴並期待它繼續運行,就好像什麼也沒發生一樣...... –
通常我們不會問問題爲你的程序的原因,但它真的引出了一個問題:你爲什麼需要這樣做? – gunr2171