• 用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(@"directoryshell", 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(@"directoryshell", 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)!

  • 相关阅读:
    hp的金牌服务
    Ubuntu小技巧
    ubuntu 下安装flash player
    Ubuntu 10.10下利用PPA源三条命令安装飞信步骤
    Ubuntu10.10下配置android的Eclipse开发环境
    ubuntu安装WineQQ
    安装phpmps
    Ubuntu下eclipse安装ADT时遇到org.eclipse.wst.sse.core 0.0.0缺失的一个解决方案
    Ubuntu 中安装 Eclipse
    HDU1007
  • 原文地址:https://www.cnblogs.com/bitzhuwei/p/FileWiper.html
Copyright © 2020-2023  润新知