自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#編程刪除系統(tǒng)自帶游戲

開發(fā) 后端
本文通過C#編程刪除了Windows2000中的四個系統(tǒng)自帶的游戲,在編寫過程中用樹狀視圖z和列表視圖方式顯示了Windows2000中隱含的DllCache目錄及其下面的文件。

  自從Windows 2000采用了動態(tài)緩存目錄的保護(hù)措施以來,通常用原來的方法在刪除系統(tǒng)自帶游戲幾秒后,游戲又可以使用了。本文介紹了在Visual Studio 2005環(huán)境下進(jìn)行C#編程,來實(shí)現(xiàn)顯示DllCache目錄下的文件,并刪除Windows 2000 pro系統(tǒng)自帶的四個游戲。

  一、界面設(shè)計

  新建Windows應(yīng)用程序,在出現(xiàn)的form中添加TreeView、ListView和Button控件各一個,調(diào)整到適當(dāng)?shù)拇笮?,改變button1的text為“刪除系統(tǒng)自帶程序”,將listview1的view項(xiàng)設(shè)置為detail,其余不變。添加三個imagelist控件,分別改名為TreeImageList、TreeViewImageList和ListViewImageList,用于存放引用自系統(tǒng)shell32.dll中的圖標(biāo)。

  二、顯示DllCache目錄及其下面的文件

  1.添加使用命名空間和文件結(jié)構(gòu)信息

  1. using System.IO;   
  2. using System.Runtime.InteropServices;   
  3. using System.Reflection;     

  2.添加文件結(jié)構(gòu)信息,調(diào)用Windows API中的提取圖標(biāo)函數(shù)和獲取系統(tǒng)路徑函數(shù),并構(gòu)造自定義的提取圖標(biāo)函數(shù)。

  1. [StructLayout(LayoutKind.Sequential)]  0 
  2.   public struct SHFILEINFO   
  3.   { public IntPtr hIcon;   
  4.   public int iIcon;   
  5.   public uint dwAttributes;   
  6.   public char szDisplayName;   
  7.   public char szTypeName; }   
  8.   private System.Windows.Forms.ImageList TreeImageList;   
  9.   //獲取圖標(biāo)   
  10.   [DllImport("Shell32.dll")]   
  11.   public static extern int ExtractIcon(IntPtr h, string strx, int ii);   
  12.   // 獲取系統(tǒng)路徑   
  13.   [DllImport("Kernel32.dll" ,CharSet = CharSet.Auto)]   
  14.   public static extern Int32 GetSystemDirectory(StringBuilder WinDir, Int32 usize);   
  15.   //構(gòu)造自定義提取圖標(biāo)函數(shù)   
  16.   protected virtual Icon myExtractIcon(string FileName, int iIndex)   
  17.   { try  
  18.   { IntPtr hIcon = (IntPtr) ExtractIcon(this.Handle, FileName, iIndex);   
  19.   if (!hIcon.Equals(null))   
  20.   { Icon icon = Icon.FromHandle(hIcon);   
  21.   return icon; }   
  22.   }   
  23.   catch (Exception ex)   
  24.   { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error); }   
  25.   return null;   
  26.   }      

  3.在Form構(gòu)造函數(shù)中添加獲取圖標(biāo)信息,圖標(biāo)取自shell32.dll。

  1. Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 15);   
  2.   TreeImageList.Images.Add(ic0);   
  3.   Icon ic1 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 5);   
  4.   TreeImageList.Images.Add(ic1);   
  5.   Icon ic2 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 7);   
  6.   TreeImageList.Images.Add(ic2);   
  7.   Icon ic3 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 11);   
  8.   TreeImageList.Images.Add(ic3);   
  9.   Icon ic4 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);   
  10.   TreeImageList.Images.Add(ic4);   
  11.   Icon ic5 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 4);   
  12.   TreeImageList.Images.Add(ic5);   
  13.   Icon ic6 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 101);   
  14.   TreeImageList.Images.Add(ic6);   
  15.   Icon ic7 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 51);      

  4.在TreeView1中顯示當(dāng)前系統(tǒng)盤符和文件目錄樹

  (1) 聲明公共變量。

  1. public const int nChars = 128;   
  2.   public StringBuilder Buff = new StringBuilder(nChars);      

  (2) 在Form構(gòu)造函數(shù)中添加下列語句,用于添加根節(jié)點(diǎn)。

  1. GetSystemDirectory(Buff, nChars);   
  2.   Buff.Remove(3, Buff.Length - 3); 
  3.   TreeNode RootNode = new TreeNode(Buff.ToString(), 0, 0);   
  4.   treeView1.BeginUpdate();   
  5.   treeView1.Nodes.Clear();   
  6.   treeView1.Nodes.Add(RootNode);   
  7.   treeView1.ImageList = TreeImageList;   
  8.   treeView1.EndUpdate();      

  (3) 選中在TreeView1的某一節(jié)點(diǎn)后,執(zhí)行AfterSelect事件中的語句,要求能夠?qū)崿F(xiàn)打開此目錄的下級目錄,并將下級目錄添加入TreeView1中。

  1. private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)   
  2.   { AddDirectories(e.Node); }//e.Node為當(dāng)前打開的節(jié)點(diǎn)   
  3.   void AddDirectories(TreeNode tn)   
  4.   {   
  5.   tn.Nodes.Clear();   
  6.   string strPath = tn.FullPath;   
  7.   DirectoryInfo dirinfo = new DirectoryInfo(strPath); //獲得當(dāng)前目錄   
  8.   DirectoryInfo[] adirinfo;   
  9.   try{ adirinfo = dirinfo.GetDirectories(); }   
  10.   catch { return; }   
  11.   int iImageIndex = 4; int iSelectedIndex = 5;   
  12.   foreach (DirectoryInfo di in adirinfo)   
  13.   {   
  14.   if (di.Name == "RECYCLER" || di.Name == "RECYCLED" || di.Name == "Recycled")   
  15.   { iImageIndex = 6; iSelectedIndex = 6; }   
  16.   else  
  17.   { iImageIndex = 4; iSelectedIndex = 5; }   
  18.   TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);   
  19.   tn.Nodes.Add(tnDir);   
  20.   }   
  21.   }      

  5.LiseView中顯示當(dāng)前目錄(選中的節(jié)點(diǎn))下的文件和下級目錄。

  (1)添加公共變量。

  1. public string strFilePath = "";  

  (2)構(gòu)造自定義函數(shù),用于顯示文件的圖標(biāo)。

  1. protected virtual void SetIcon(ImageList imageList, string FileName, bool tf)   
  2.   { SHFILEINFO fi = new SHFILEINFO();   
  3.   if (tf == true)   
  4.   { int iTotal = (int)SHGetFileInfo(FileName, 0, ref fi, 100, 16640);   
  5.   try  
  6.   { if (iTotal > 0)   
  7.   { Icon ic = Icon.FromHandle(fi.hIcon); //提取文件自帶的小圖標(biāo)   
  8.   imageList.Images.Add(ic); }   
  9.   }   
  10.   catch (Exception ex)   
  11.   { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error); }   
  12.   }   
  13.   else  
  14.   { int iTotal = (int)SHGetFileInfo(FileName, 0, ref fi, 100, 257);   
  15.   try  
  16.   { if (iTotal > 0)   
  17.   { Icon ic = Icon.FromHandle(fi.hIcon);   
  18.   imageList.Images.Add(ic); }   
  19.   }   
  20.   catch (Exception ex)   
  21.   { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error);
  22.   }   
  23.   }   
  24.   }      

  (3) 構(gòu)造自定義函數(shù),用于顯示選中的基本節(jié)點(diǎn)下的文件和下級目錄。

  1. protected virtual void InitList(TreeNode tn)   
  2.   { this.Cursor = Cursors.WaitCursor;   
  3.   this.ListViewImageList.Images.Clear();   
  4.   listView1.SmallImageList = this.ListViewImageList;   
  5.   Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);   
  6.   this.ListViewImageList.Images.Add(ic0);   
  7.   listView1.Clear();   
  8.   //設(shè)置列表框的表頭   
  9.   listView1.Columns.Add("文件(夾)名", 160, HorizontalAlignment.Left);   
  10.   listView1.Columns.Add("擴(kuò)展名", 100, HorizontalAlignment.Center);   
  11.   listView1.Columns.Add("文件大小", 120, HorizontalAlignment.Left);   
  12.   listView1.Columns.Add("創(chuàng)建時間", 120, HorizontalAlignment.Left);   
  13.   listView1.Columns.Add("訪問時間", 200, HorizontalAlignment.Left);   
  14.   listView1.Columns.Add("上級文件夾", 400, HorizontalAlignment.Left); 
  15.   string strPath = tn.FullPath;   
  16.   //獲得當(dāng)前目錄下的所有文件   
  17.   DirectoryInfo curDir = new DirectoryInfo(strPath);//創(chuàng)建目錄對象。   
  18.   FileInfo[] dirFiles;   
  19.   try { dirFiles = curDir.GetFiles(); }   
  20.   catch { return; }   
  21.   string[] arrSubItem = new string[10];   
  22.   //文件的創(chuàng)建時間和訪問時間。   
  23.   int iCount = 0;   
  24.   int iconIndex = 1;//用1,而不用0是要讓過0號圖標(biāo)。   
  25.   foreach (FileInfo fileInfo in dirFiles)   
  26.   { string strFileName = fileInfo.Name;   
  27.   //如果不是文件pagefile.sys   
  28.   if (!strFileName.Equals("pagefile.sys"))   
  29.   { arrSubItem[0] = strFileName;   
  30.   if (fileInfo.Extension.Trim() == "")   
  31.   arrSubItem[1] = "未知類型";   
  32.   else  
  33.   arrSubItem[1] = fileInfo.Extension.ToString();   
  34.   arrSubItem[2] = fileInfo.Length + "字節(jié)";   
  35.   arrSubItem[3] = fileInfo.CreationTime.ToString();   
  36.   arrSubItem[4] = fileInfo.LastAccessTime.ToString(); 
  37.   arrSubItem[5] = fileInfo.Directory.ToString(); }   
  38.   else  
  39.   { arrSubItem[1] = "未知擴(kuò)展名";   
  40.   arrSubItem[2] = "未知大小";   
  41.   arrSubItem[3] = "未知日期";   
  42.   arrSubItem[4] = "未知日期";   
  43.   arrSubItem[5] = "未知上級文件夾"; }   
  44.   //得到每個文件的圖標(biāo)   
  45.   string str = fileInfo.FullName;   
  46.   try { SetIcon(this.ListViewImageList, str, true); }   
  47.   catch (Exception ex)   
  48.   { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error); }   
  49.   //插入列表項(xiàng)   
  50.   ListViewItem LiItem = new ListViewItem(arrSubItem, iconIndex);   
  51.   listView1.Items.Insert(iCount, LiItem);   
  52.   iCount++;   
  53.   iconIndex++;   
  54.   }   
  55.   strFilePath = strPath;   
  56.   this.Cursor = Cursors.Arrow;   
  57.   //以下是向列表框中插入目錄,不是文件。獲得當(dāng)前目錄下的各個子目錄。   
  58.   int iItem = 0;   
  59.   DirectoryInfo Dir = new DirectoryInfo(strPath);   
  60.   string[] arrDirectorySubItem = new string[10];   
  61.   foreach (DirectoryInfo di in Dir.GetDirectories())   
  62.   { arrDirectorySubItem[0] = di.Name;   
  63.   if (di.Extension.Trim() != "")   
  64.   arrDirectorySubItem[1] = di.Extension;   
  65.   else  
  66.   { arrDirectorySubItem[1] = "   
  67.   ";   
  68.   arrDirectorySubItem[2] = "";   
  69.   arrDirectorySubItem[3] = "";   
  70.   arrDirectorySubItem[4] = "";   
  71.   arrDirectorySubItem[5] = ""; } 
  72.   ListViewItem LiItem = new ListViewItem(arrDirectorySubItem, 0);   
  73.   listView1.Items.Insert(iItem, LiItem);   
  74.   iItem++;   
  75.   }   
  76.   }      

  (4) 在構(gòu)造自定treeView1_AfterSelect中的“AddDirectories(e.Node);”語句后添加下語句。

  1. InitList(e.Node);      

#p#

三、刪除系統(tǒng)自帶的四個游戲程序

  (1)自定義函數(shù),用于刪除Windows2000的四個系統(tǒng)自帶游戲

  1. private void DelSystemFourGames()   
  2.   { string str="";   
  3.   StringBuilder buff1 = new StringBuilder(nChars);   
  4.   StringBuilder buff2 = new StringBuilder(nChars);   
  5.   GetSystemDirectory(Buff, nChars);   
  6.   Buff.Append("\\");   
  7.   GetSystemDirectory(buff1, nChars);   
  8.   buff1.Append("\\");   
  9.   buff2=buff1;   
  10.   str="sol.exe"; 
  11.   if(File_in_Directory(str, buff1.ToString()))   
  12.   { Buff.Append("sol.exe");//紙牌   
  13.   buff2.Append("DllCache\\");   
  14.   buff2.Append("sol.exe");   
  15.   //執(zhí)行刪除文件,刪除后的文件不出現(xiàn)在回收站中   
  16.   File.Delete(Buff.ToString());   
  17.   File.Delete(buff2.ToString());  
  18.   Buff.Remove(Buff.Length - 7, 7);   
  19.   //還原Buff的字符為system32\目錄下,7是“sol.exe”的長度   
  20.   buff2.Remove(buff2.Length - 7, 7);//類上,還原為dllcache\目錄下   
  21.   }   
  22.   …… 
  23.   //省略了刪除“空當(dāng)接龍”和“掃雷”兩個游戲的程序段因其內(nèi)容同上,只不過改str = "freecell.exe"   
  24.   //和str = "winmine.exe",以及Buff.Remove中的數(shù)字長度與相應(yīng)的文件名長度一致。   
  25.   // 刪除windows XP中的蜘蛛“spider.exe”與上類同   
  26.   GetSystemDirectory(Buff, nChars);   
  27.   GetSystemDirectory(buff2, nChars);  
  28.   buff2.Append("\\");   
  29.   Buff.Remove(3, Buff.Length - 3); //反回到“盤符:\”狀態(tài)   
  30.   Buff.Append("Program Files\\WIndows NT\\Pinball");//桌上彈球   
  31.   str = "pinball.exe";   
  32.   if (File_in_Directory(str, Buff.ToString()))   
  33.   { DeleteDir(Buff.ToString());//刪除目錄   
  34.   buff2.Append("DllCache\\");   
  35.   buff2.Append("pinball.exe");  
  36.   File.Delete(buff2.ToString()); 
  37.   }   
  38.   }      

  (2)在button1_OnClick中調(diào)用自定義刪除函數(shù)

  1. DelSystemFourGames(); 

四、兩個自定義函數(shù)

  1.判斷文件是否在指定的文件夾中

  1. private bool File_in_Directory(string str1, string str2)   
  2.   {   
  3.   DirectoryInfo curDir = new DirectoryInfo(str2);//創(chuàng)建目錄對象。   
  4.   FileInfo[] dirFiles;   
  5.   try  
  6.   { dirFiles = curDir.GetFiles(); }   
  7.   catch  
  8.   { return false; }   
  9.   foreach (FileInfo fileInfo in dirFiles)   
  10.   { if (fileInfo.Name == str1) return true; }   
  11.   return false;   
  12.   }      

  2.刪除目錄及目錄下所有文件與子目錄

  1. public static void DeleteDir(string Path)   
  2.   { try  
  3.   { // 檢查路徑名是否以分割字符結(jié)束,如果不是則添加”\”分隔符   
  4.   if (Path[Path.Length - 1] != Path.DirectorySeparatorChar)   
  5.   Path += Path.DirectorySeparatorChar;   
  6.   string[] fileList = Directory.GetFileSystemEntries(Path);   
  7.   // 遍歷所有的文件和目錄   
  8.   foreach (string file in fileList)   
  9.   {   
  10.   // 先將文件當(dāng)作目錄處理如果存在這個目錄就遞歸Delete該目錄下面的文件   
  11.   if (Directory.Exists(file))   
  12.   {   
  13.   DeleteDir(Path + Path.GetFileName(file));   
  14.   }   
  15.   else // 否則直接Delete文件   
  16.   { //改變文件的只讀屬性   
  17.   FileInfo fi = new FileInfo(file);   
  18.   if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)   
  19.   fi.Attributes = FileAttributes.Normal;   
  20.   File.Delete(Path + Path.GetFileName(file)); //刪除文件   
  21.   }   
  22.   }   
  23.   System.IO.Directory.Delete(Path, true); //刪除文件夾   
  24.   }   
  25.   catch (Exception e)   
  26.   { MessageBox.Show(e.ToString()); }   
  27.   }  

  附言:本文程序采用的是Visual Studio 2005 C#編寫,所述代碼均已在Windows 2000 pro/server中運(yùn)行通過。

  本文通過C#編程實(shí)現(xiàn)了刪除Windows 2000系統(tǒng)自帶游戲這個目標(biāo),并將微軟為考慮自身安全的dllcache目錄及其中的文件顯示出來,希望能夠?qū)σ私膺@方面的相關(guān)人員有所幫助。

【編輯推薦】

  1. 教你寫不可思議的C#代碼
  2. C#調(diào)試從入門到精通
  3. 詳解C#中如何訪問私有成員
  4. 編程必讀 15個編程好習(xí)慣
  5. 程序員編程須知的七大攻略
責(zé)任編輯:韓亞珊 來源: 電腦編程技巧與維護(hù)雜志社
相關(guān)推薦

2009-08-17 08:04:00

C#高級編程

2009-09-09 18:20:29

C# XML編程

2009-08-24 11:02:52

C#接口映射

2009-08-26 10:34:15

C#類型C#變量

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2012-09-24 15:35:24

C#網(wǎng)絡(luò)協(xié)議UDP

2012-09-24 15:13:50

C#網(wǎng)絡(luò)協(xié)議TCP

2009-08-27 16:30:08

C#編程命名規(guī)范

2011-04-13 17:31:16

C#.NET

2009-08-27 14:12:02

C# interfac

2009-08-19 15:18:53

迭代器

2021-10-12 17:47:22

C# TAP異步

2009-08-14 16:08:34

讀寫B(tài)inaryC#編程實(shí)例

2024-03-04 18:49:59

反射C#開發(fā)

2009-08-25 17:13:57

C#串口編程

2009-09-11 09:10:30

C#編寫游戲

2009-09-01 16:12:41

C#命名指南

2015-09-16 15:11:58

C#異步編程

2009-08-31 18:17:32

C#接口編程

2009-08-24 10:06:31

C#接口成員
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號