C#編程刪除系統(tǒng)自帶游戲
自從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)信息
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Reflection;
2.添加文件結(jié)構(gòu)信息,調(diào)用Windows API中的提取圖標(biāo)函數(shù)和獲取系統(tǒng)路徑函數(shù),并構(gòu)造自定義的提取圖標(biāo)函數(shù)。
- [StructLayout(LayoutKind.Sequential)] 0
- public struct SHFILEINFO
- { public IntPtr hIcon;
- public int iIcon;
- public uint dwAttributes;
- public char szDisplayName;
- public char szTypeName; }
- private System.Windows.Forms.ImageList TreeImageList;
- //獲取圖標(biāo)
- [DllImport("Shell32.dll")]
- public static extern int ExtractIcon(IntPtr h, string strx, int ii);
- // 獲取系統(tǒng)路徑
- [DllImport("Kernel32.dll" ,CharSet = CharSet.Auto)]
- public static extern Int32 GetSystemDirectory(StringBuilder WinDir, Int32 usize);
- //構(gòu)造自定義提取圖標(biāo)函數(shù)
- protected virtual Icon myExtractIcon(string FileName, int iIndex)
- { try
- { IntPtr hIcon = (IntPtr) ExtractIcon(this.Handle, FileName, iIndex);
- if (!hIcon.Equals(null))
- { Icon icon = Icon.FromHandle(hIcon);
- return icon; }
- }
- catch (Exception ex)
- { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error); }
- return null;
- }
3.在Form構(gòu)造函數(shù)中添加獲取圖標(biāo)信息,圖標(biāo)取自shell32.dll。
- Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 15);
- TreeImageList.Images.Add(ic0);
- Icon ic1 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 5);
- TreeImageList.Images.Add(ic1);
- Icon ic2 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 7);
- TreeImageList.Images.Add(ic2);
- Icon ic3 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 11);
- TreeImageList.Images.Add(ic3);
- Icon ic4 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);
- TreeImageList.Images.Add(ic4);
- Icon ic5 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 4);
- TreeImageList.Images.Add(ic5);
- Icon ic6 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 101);
- TreeImageList.Images.Add(ic6);
- Icon ic7 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 51);
4.在TreeView1中顯示當(dāng)前系統(tǒng)盤符和文件目錄樹
(1) 聲明公共變量。
- public const int nChars = 128;
- public StringBuilder Buff = new StringBuilder(nChars);
(2) 在Form構(gòu)造函數(shù)中添加下列語句,用于添加根節(jié)點(diǎn)。
- GetSystemDirectory(Buff, nChars);
- Buff.Remove(3, Buff.Length - 3);
- TreeNode RootNode = new TreeNode(Buff.ToString(), 0, 0);
- treeView1.BeginUpdate();
- treeView1.Nodes.Clear();
- treeView1.Nodes.Add(RootNode);
- treeView1.ImageList = TreeImageList;
- treeView1.EndUpdate();
(3) 選中在TreeView1的某一節(jié)點(diǎn)后,執(zhí)行AfterSelect事件中的語句,要求能夠?qū)崿F(xiàn)打開此目錄的下級目錄,并將下級目錄添加入TreeView1中。
- private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
- { AddDirectories(e.Node); }//e.Node為當(dāng)前打開的節(jié)點(diǎn)
- void AddDirectories(TreeNode tn)
- {
- tn.Nodes.Clear();
- string strPath = tn.FullPath;
- DirectoryInfo dirinfo = new DirectoryInfo(strPath); //獲得當(dāng)前目錄
- DirectoryInfo[] adirinfo;
- try{ adirinfo = dirinfo.GetDirectories(); }
- catch { return; }
- int iImageIndex = 4; int iSelectedIndex = 5;
- foreach (DirectoryInfo di in adirinfo)
- {
- if (di.Name == "RECYCLER" || di.Name == "RECYCLED" || di.Name == "Recycled")
- { iImageIndex = 6; iSelectedIndex = 6; }
- else
- { iImageIndex = 4; iSelectedIndex = 5; }
- TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);
- tn.Nodes.Add(tnDir);
- }
- }
5.LiseView中顯示當(dāng)前目錄(選中的節(jié)點(diǎn))下的文件和下級目錄。
(1)添加公共變量。
- public string strFilePath = "";
(2)構(gòu)造自定義函數(shù),用于顯示文件的圖標(biāo)。
- protected virtual void SetIcon(ImageList imageList, string FileName, bool tf)
- { SHFILEINFO fi = new SHFILEINFO();
- if (tf == true)
- { int iTotal = (int)SHGetFileInfo(FileName, 0, ref fi, 100, 16640);
- try
- { if (iTotal > 0)
- { Icon ic = Icon.FromHandle(fi.hIcon); //提取文件自帶的小圖標(biāo)
- imageList.Images.Add(ic); }
- }
- catch (Exception ex)
- { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error); }
- }
- else
- { int iTotal = (int)SHGetFileInfo(FileName, 0, ref fi, 100, 257);
- try
- { if (iTotal > 0)
- { Icon ic = Icon.FromHandle(fi.hIcon);
- imageList.Images.Add(ic); }
- }
- catch (Exception ex)
- { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error);
- }
- }
- }
(3) 構(gòu)造自定義函數(shù),用于顯示選中的基本節(jié)點(diǎn)下的文件和下級目錄。
- protected virtual void InitList(TreeNode tn)
- { this.Cursor = Cursors.WaitCursor;
- this.ListViewImageList.Images.Clear();
- listView1.SmallImageList = this.ListViewImageList;
- Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);
- this.ListViewImageList.Images.Add(ic0);
- listView1.Clear();
- //設(shè)置列表框的表頭
- listView1.Columns.Add("文件(夾)名", 160, HorizontalAlignment.Left);
- listView1.Columns.Add("擴(kuò)展名", 100, HorizontalAlignment.Center);
- listView1.Columns.Add("文件大小", 120, HorizontalAlignment.Left);
- listView1.Columns.Add("創(chuàng)建時間", 120, HorizontalAlignment.Left);
- listView1.Columns.Add("訪問時間", 200, HorizontalAlignment.Left);
- listView1.Columns.Add("上級文件夾", 400, HorizontalAlignment.Left);
- string strPath = tn.FullPath;
- //獲得當(dāng)前目錄下的所有文件
- DirectoryInfo curDir = new DirectoryInfo(strPath);//創(chuàng)建目錄對象。
- FileInfo[] dirFiles;
- try { dirFiles = curDir.GetFiles(); }
- catch { return; }
- string[] arrSubItem = new string[10];
- //文件的創(chuàng)建時間和訪問時間。
- int iCount = 0;
- int iconIndex = 1;//用1,而不用0是要讓過0號圖標(biāo)。
- foreach (FileInfo fileInfo in dirFiles)
- { string strFileName = fileInfo.Name;
- //如果不是文件pagefile.sys
- if (!strFileName.Equals("pagefile.sys"))
- { arrSubItem[0] = strFileName;
- if (fileInfo.Extension.Trim() == "")
- arrSubItem[1] = "未知類型";
- else
- arrSubItem[1] = fileInfo.Extension.ToString();
- arrSubItem[2] = fileInfo.Length + "字節(jié)";
- arrSubItem[3] = fileInfo.CreationTime.ToString();
- arrSubItem[4] = fileInfo.LastAccessTime.ToString();
- arrSubItem[5] = fileInfo.Directory.ToString(); }
- else
- { arrSubItem[1] = "未知擴(kuò)展名";
- arrSubItem[2] = "未知大小";
- arrSubItem[3] = "未知日期";
- arrSubItem[4] = "未知日期";
- arrSubItem[5] = "未知上級文件夾"; }
- //得到每個文件的圖標(biāo)
- string str = fileInfo.FullName;
- try { SetIcon(this.ListViewImageList, str, true); }
- catch (Exception ex)
- { MessageBox.Show(ex.Message, "錯誤提示", 0, MessageBoxIcon.Error); }
- //插入列表項(xiàng)
- ListViewItem LiItem = new ListViewItem(arrSubItem, iconIndex);
- listView1.Items.Insert(iCount, LiItem);
- iCount++;
- iconIndex++;
- }
- strFilePath = strPath;
- this.Cursor = Cursors.Arrow;
- //以下是向列表框中插入目錄,不是文件。獲得當(dāng)前目錄下的各個子目錄。
- int iItem = 0;
- DirectoryInfo Dir = new DirectoryInfo(strPath);
- string[] arrDirectorySubItem = new string[10];
- foreach (DirectoryInfo di in Dir.GetDirectories())
- { arrDirectorySubItem[0] = di.Name;
- if (di.Extension.Trim() != "")
- arrDirectorySubItem[1] = di.Extension;
- else
- { arrDirectorySubItem[1] = "
- ";
- arrDirectorySubItem[2] = "";
- arrDirectorySubItem[3] = "";
- arrDirectorySubItem[4] = "";
- arrDirectorySubItem[5] = ""; }
- ListViewItem LiItem = new ListViewItem(arrDirectorySubItem, 0);
- listView1.Items.Insert(iItem, LiItem);
- iItem++;
- }
- }
(4) 在構(gòu)造自定treeView1_AfterSelect中的“AddDirectories(e.Node);”語句后添加下語句。
- InitList(e.Node);
#p#
三、刪除系統(tǒng)自帶的四個游戲程序
(1)自定義函數(shù),用于刪除Windows2000的四個系統(tǒng)自帶游戲
- private void DelSystemFourGames()
- { string str="";
- StringBuilder buff1 = new StringBuilder(nChars);
- StringBuilder buff2 = new StringBuilder(nChars);
- GetSystemDirectory(Buff, nChars);
- Buff.Append("\\");
- GetSystemDirectory(buff1, nChars);
- buff1.Append("\\");
- buff2=buff1;
- str="sol.exe";
- if(File_in_Directory(str, buff1.ToString()))
- { Buff.Append("sol.exe");//紙牌
- buff2.Append("DllCache\\");
- buff2.Append("sol.exe");
- //執(zhí)行刪除文件,刪除后的文件不出現(xiàn)在回收站中
- File.Delete(Buff.ToString());
- File.Delete(buff2.ToString());
- Buff.Remove(Buff.Length - 7, 7);
- //還原Buff的字符為system32\目錄下,7是“sol.exe”的長度
- buff2.Remove(buff2.Length - 7, 7);//類上,還原為dllcache\目錄下
- }
- ……
- //省略了刪除“空當(dāng)接龍”和“掃雷”兩個游戲的程序段因其內(nèi)容同上,只不過改str = "freecell.exe"
- //和str = "winmine.exe",以及Buff.Remove中的數(shù)字長度與相應(yīng)的文件名長度一致。
- // 刪除windows XP中的蜘蛛“spider.exe”與上類同
- GetSystemDirectory(Buff, nChars);
- GetSystemDirectory(buff2, nChars);
- buff2.Append("\\");
- Buff.Remove(3, Buff.Length - 3); //反回到“盤符:\”狀態(tài)
- Buff.Append("Program Files\\WIndows NT\\Pinball");//桌上彈球
- str = "pinball.exe";
- if (File_in_Directory(str, Buff.ToString()))
- { DeleteDir(Buff.ToString());//刪除目錄
- buff2.Append("DllCache\\");
- buff2.Append("pinball.exe");
- File.Delete(buff2.ToString());
- }
- }
(2)在button1_OnClick中調(diào)用自定義刪除函數(shù)
- DelSystemFourGames();
四、兩個自定義函數(shù)
1.判斷文件是否在指定的文件夾中
- private bool File_in_Directory(string str1, string str2)
- {
- DirectoryInfo curDir = new DirectoryInfo(str2);//創(chuàng)建目錄對象。
- FileInfo[] dirFiles;
- try
- { dirFiles = curDir.GetFiles(); }
- catch
- { return false; }
- foreach (FileInfo fileInfo in dirFiles)
- { if (fileInfo.Name == str1) return true; }
- return false;
- }
2.刪除目錄及目錄下所有文件與子目錄
- public static void DeleteDir(string Path)
- { try
- { // 檢查路徑名是否以分割字符結(jié)束,如果不是則添加”\”分隔符
- if (Path[Path.Length - 1] != Path.DirectorySeparatorChar)
- Path += Path.DirectorySeparatorChar;
- string[] fileList = Directory.GetFileSystemEntries(Path);
- // 遍歷所有的文件和目錄
- foreach (string file in fileList)
- {
- // 先將文件當(dāng)作目錄處理如果存在這個目錄就遞歸Delete該目錄下面的文件
- if (Directory.Exists(file))
- {
- DeleteDir(Path + Path.GetFileName(file));
- }
- else // 否則直接Delete文件
- { //改變文件的只讀屬性
- FileInfo fi = new FileInfo(file);
- if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
- fi.Attributes = FileAttributes.Normal;
- File.Delete(Path + Path.GetFileName(file)); //刪除文件
- }
- }
- System.IO.Directory.Delete(Path, true); //刪除文件夾
- }
- catch (Exception e)
- { MessageBox.Show(e.ToString()); }
- }
附言:本文程序采用的是Visual Studio 2005 C#編寫,所述代碼均已在Windows 2000 pro/server中運(yùn)行通過。
本文通過C#編程實(shí)現(xiàn)了刪除Windows 2000系統(tǒng)自帶游戲這個目標(biāo),并將微軟為考慮自身安全的dllcache目錄及其中的文件顯示出來,希望能夠?qū)σ私膺@方面的相關(guān)人員有所幫助。
【編輯推薦】