用C#Winform寫個簡單的批量清空文件內容和刪除文件的小工具


C#Winform寫個簡單的批量清空文件內容和刪除文件的小工具

本文介紹這個簡單得不能再簡單的小項目。做這個項目,有以下目的。

當然是做個能用的工具

學習使用Github

 

關於用VS2013創建一個項目並添加到Github的教程,請參考(http://www.admin10000.com/document/4004.html)。簡單來說,就是先用VS創建項目;然后在Github網站上創建一個Respo(本項目的代碼托管項目),記下(https://*.git)那個地址;最后用"提交""同步"這些按鈕來同步代碼到服務器。

 

這個小工具我命名為FileWiper,其Github地址為(https://github.com/bitzhuwei/FileWiper.git),歡迎大家來討論。

使用Github

有一個倉庫,叫Repo A。你如果要往里貢獻代碼,首先要Fork這個Repo,於是在你的Github賬號下有了一個Repo A2,。然后你在這個A2下工作,Commit,push等。然后你希望原始倉庫Repo A合並你的工作,你可以在Github上發起一個Pull Request,意思是請求Repo A的所有者從你的A2合並分支。如果被審核通過並正式合並,這樣你就為項目A做貢獻了

清空文件內容

只需打開文件,將原來的內容覆蓋掉即可。要想徹底消滅文件數據,最好就是銷毀其中的內容,然后把文件名改掉,最后再刪除文件。這樣,即使文件被什么工具恢復了,也只是文件名沒有意義,內容為空的一個無用的東西。

public static void WipeFileContent(string filename)

{

     using (var stream =

        new System.IO.StreamWriter(filename, false))

     {

         stream.Write("http://bitzhuwei.cnblogs.com");

     }

}

 

注冊到系統右鍵菜單

用RegistryKey進行注冊。

程序路徑后面跟了個" %1",這樣在啟動時,在Main函數里的args參數就會包含選定的文件路徑(或文件夾路徑)。

 1         private void btnRegister_Click(object sender, EventArgs e)
 2         {
 3             //給所有類型的文件添加自定義的右鍵菜單
 4             {
 5                 var itemName = "Wipe Content";
 6                 var associatedProgramFullPath = this.GetType().Assembly.Location;
 7                 //創建項:shell 
 8                 RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true);
 9                 if (shellKey == null)
10                 {
11                     shellKey = Registry.ClassesRoot.CreateSubKey(@"*\shell");
12                 }
13 
14                 //創建項:右鍵顯示的菜單名稱
15                 RegistryKey rightCommondKey = shellKey.CreateSubKey(itemName);
16                 RegistryKey associatedProgramKey = rightCommondKey.CreateSubKey("command");
17 
18                 //創建默認值:關聯的程序
19                 associatedProgramKey.SetValue(string.Empty, associatedProgramFullPath + " %1");
20 
21                 //刷新到磁盤並釋放資源
22                 associatedProgramKey.Close();
23                 rightCommondKey.Close();
24                 shellKey.Close();
25             }
26 
27             //給所有文件夾添加自定義的右鍵菜單
28             {
29                 var itemName = "Wipe Directory";
30                 var associatedProgramFullPath = this.GetType().Assembly.Location;
31                 //創建項:shell 
32                 RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"directory\shell", true);
33                 if (shellKey == null)
34                 {
35                     shellKey = Registry.ClassesRoot.CreateSubKey(@"*\shell");
36                 }
37 
38                 //創建項:右鍵顯示的菜單名稱
39                 RegistryKey rightCommondKey = shellKey.CreateSubKey(itemName);
40                 RegistryKey associatedProgramKey = rightCommondKey.CreateSubKey("command");
41 
42                 //創建默認值:關聯的程序
43                 associatedProgramKey.SetValue("", associatedProgramFullPath +" %1");
44 
45 
46                 //刷新到磁盤並釋放資源
47                 associatedProgramKey.Close();
48                 rightCommondKey.Close();
49                 shellKey.Close();
50             }
51         }
注冊到系統右鍵菜單

 

取消注冊系統右鍵菜單

仍然用RegistryKey實現。

 1         private void btnUnregister_Click(object sender, EventArgs e)
 2         {
 3             //給所有類型的文件刪除自定義的右鍵菜單
 4             {
 5                 var itemName = "Wipe Content";
 6                 var associatedProgramFullPath = this.GetType().Assembly.Location;
 7                 //創建項:shell 
 8                 RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true);
 9                 if(shellKey!=null)
10                 {
11                     shellKey.DeleteSubKeyTree(itemName, false);
12                 }
13 
14                 //刷新到磁盤並釋放資源
15                 shellKey.Close();
16             }
17 
18             //給所有文件夾刪除自定義的右鍵菜單
19             {
20                 var itemName = "Wipe Directory";
21                 var associatedProgramFullPath = this.GetType().Assembly.Location;
22                 //創建項:shell 
23                 RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"directory\shell", true);
24                 if (shellKey != null)
25                 {
26                     shellKey.DeleteSubKeyTree(itemName, false);
27                 }
28 
29                 //刷新到磁盤並釋放資源
30                 shellKey.Close();
31             }
32         }
取消注冊系統右鍵菜單

 

歡迎大家到本項目的github上關注(https://github.com/bitzhuwei/FileWiper.git)!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM