Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete the game that comes with the system in C # programming

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to delete system-built games in C# programming". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Interface design

Create a new Windows application, add a TreeView, ListView and Button control to the form that appears, adjust it to the appropriate size, change the text of button1 to "remove system-built-in programs", set the view entry of listview1 to detail, and the rest remain unchanged. Add three imagelist controls, renamed TreeImageList, TreeViewImageList, and ListViewImageList, to hold icons referenced from the system shell32.dll.

Display the DllCache directory and the files below it

1. Add information about using namespaces and file structure

Using System.IO; using System.Runtime.InteropServices; using System.Reflection

two。 Add file structure information, call the extraction icon function and get the system path function in Windows API, and construct a custom extraction icon function.

[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; / / get icon [DllImport ("Shell32.dll")] public static extern int ExtractIcon (IntPtr h, string strx, int ii) / / get the system path [DllImport ("Kernel32.dll", CharSet = CharSet.Auto)] public static extern Int32 GetSystemDirectory (StringBuilder WinDir, Int32 usize); / / construct a custom extraction icon function 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, "error prompt", 0, MessageBoxIcon.Error);} return null;}

3. Add the get icon information in the Form constructor, which is taken from 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. Display the current system drive letter and file directory tree in TreeView1

(1) declare public variables.

Public const int nChars = 128; public StringBuilder Buff = new StringBuilder (nChars)

(2) add the following statement to the Form constructor to add the root node.

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) after a node in TreeView1 is selected, the statement in the AfterSelect event is executed, which requires that the subordinate directory of this directory can be opened and added to the TreeView1.

Private void treeView1_AfterSelect (object sender, TreeViewEventArgs e) {AddDirectories (e.Node);} / e.Node is the currently open node void AddDirectories (TreeNode tn) {tn.Nodes.Clear (); string strPath = tn.FullPath; DirectoryInfo dirinfo = new DirectoryInfo (strPath); / / get the current directory 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);}

The files and subordinate directories under the current directory (selected node) are displayed in 5.LiseView.

(1) add public variables.

Public string strFilePath = ""

(2) construct a custom function to display the icon of the file.

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,100); try {if (iTotal > 0) {Icon ic = Icon.FromHandle (fi.hIcon); / / extract the small icon imageList.Images.Add (ic) that comes with the file } catch (Exception ex) {MessageBox.Show (ex.Message, "error prompt", 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, "error prompt", 0, MessageBoxIcon.Error);}

(3) construct a custom function to display files and subordinate directories under the selected base node.

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 (); / / set the header listView1.Columns.Add of the list box ("File (folder) name", 160,HorizontalAlignment.Left) ListView1.Columns.Add ("extension", 100,100,100); listView1.Columns.Add ("File size", 120,120,120); listView1.Columns.Add ("creation time", 120,120); listView1.Columns.Add ("access time", 200,200, HorizontalAlignment.Left); listView1.Columns.Add ("parent folder", 400,HorizontalAlignment.Left); string strPath = tn.FullPath / / get all the files in the current directory DirectoryInfo curDir = new DirectoryInfo (strPath); / / create directory objects. FileInfo [] dirFiles; try {dirFiles = curDir.GetFiles ();} catch {return;} string [] arrSubItem = new string [10]; / the creation time and access time of the file. Int iCount = 0; int iconIndex = 1 / use 1 instead of 0 is to pass the 0 icon. Foreach (FileInfo fileInfo in dirFiles) {string strFileName = fileInfo.Name; / / if it is not a file pagefile.sys if (! strFileName.Equals ("pagefile.sys")) {arrSubItem [0] = strFileName; if (fileInfo.Extension.Trim () = ") arrSubItem [1] =" unknown type "; else arrSubItem [1] = fileInfo.Extension.ToString (); arrSubItem [2] = fileInfo.Length +" bytes "; arrSubItem [3] = fileInfo.CreationTime.ToString () ArrSubItem [4] = fileInfo.LastAccessTime.ToString (); arrSubItem [5] = fileInfo.Directory.ToString ();} else {arrSubItem [1] = "unknown extension"; arrSubItem [2] = "unknown size"; arrSubItem [3] = "unknown date"; arrSubItem [4] = "unknown date"; arrSubItem [5] = "unknown parent folder";} / / get the icon for each file string str = fileInfo.FullName Try {SetIcon (this.ListViewImageList, str, true);} catch (Exception ex) {MessageBox.Show (ex.Message, "error prompt", 0, MessageBoxIcon.Error);} / insert list item ListViewItem LiItem = new ListViewItem (arrSubItem, iconIndex); listView1.Items.Insert (iCount, LiItem); iCount++; iconIndex++;} strFilePath = strPath; this.Cursor = Cursors.Arrow; / / the following is an insert directory into the list box, not a file. Get each subdirectory under the current directory. 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) add the following statement after constructing the "AddDirectories (e.Node);" statement in the custom treeView1_AfterSelect.

InitList (e.Node)

Delete the four game programs that come with the system

(1) Custom function, which is used to delete the games provided by the four systems of Windows2000

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 "); / / Solitaire buff2.Append (" DllCache\ ") Buff2.Append ("sol.exe"); / / delete files. The deleted files do not appear in the Recycle Bin: File.Delete (Buff.ToString ()); File.Delete (buff2.ToString ()); Buff.Remove (Buff.Length-7,7); / / restore the Buff character to the system32\ directory, and 7 is the length buff2.Remove of "sol.exe" (buff2.Length-7,7). / / on the class, restore to the dllcache\ directory}. / / omitted the program segments of the two games of "empty Dragon" and "Minesweeper" because their contents are the same as above, but changed to str = "freecell.exe" / / and str = "winmine.exe", and the number length in Buff.Remove is the same as the corresponding file name length. / delete the spider "spider.exe" in windows XP with the same GetSystemDirectory (Buff, nChars); GetSystemDirectory (buff2, nChars); buff2.Append ("\\"); Buff.Remove (3, Buff.Length-3); / / return to "disk letter:\" status Buff.Append ("Program Files\\ WIndows NT\\ Pinball"); / / Table pinball str = "pinball.exe" If (File_in_Directory (str, Buff.ToString ()) {DeleteDir (Buff.ToString ()); / / Delete directory buff2.Append ("DllCache\"); buff2.Append ("pinball.exe"); File.Delete (buff2.ToString ());}}

(2) call the custom deletion function in button1_OnClick

DelSystemFourGames ()

Four, two custom functions

1. Determine whether the file is in the specified folder

Private bool File_in_Directory (string str1, string str2) {DirectoryInfo curDir = new DirectoryInfo (str2); / / create a directory object. FileInfo [] dirFiles; try {dirFiles = curDir.GetFiles ();} catch {return false;} foreach (FileInfo fileInfo in dirFiles) {if (fileInfo.Name = = str1) return true;} return false;}

two。 Delete the directory and all files and subdirectories under the directory

Public static void DeleteDir (string Path) {try {/ / check whether the pathname ends with a split character, and if not, add the "\" delimiter if (Path [Path.Length-1]! = Path.DirectorySeparatorChar) Path + = Path.DirectorySeparatorChar; string [] fileList = Directory.GetFileSystemEntries (Path) / / iterate through all files and directories foreach (string file in fileList) {/ / first treat files as directories. If this directory exists, recursively Delete the files under that directory if (Directory.Exists (file)) {DeleteDir (Path + Path.GetFileName (file));} else / / otherwise directly Delete files {/ / change the read-only attribute of the file FileInfo fi = new FileInfo (file) If (fi.Attributes.ToString (). IndexOf ("ReadOnly")! =-1) fi.Attributes = FileAttributes.Normal; File.Delete (Path + Path.GetFileName (file)); / / delete files}} System.IO.Directory.Delete (Path, true); / / delete folder} catch (Exception e) {MessageBox.Show (e.ToString ()) This is the end of the introduction of "how to delete the game that comes with the system in C# programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 226

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report