数据恢复 2

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         //检查文件是否存在  
  6.         if (!File.Exists(textBox1.Text.ToString()))  
  7.         {  
  8.             MessageBox.Show("输入文件不存在!");  
  9.         }  
  10.         else if (Convert.ToInt32(textBox2.Text) < 1)  //覆盖次数大于1  
  11.         {  
  12.             MessageBox.Show("覆盖次数要大于1");  
  13.         }  
  14.         else  
  15.         {  
  16.             //初始化进程 命名空间 using System.Diagnostics  
  17.             Process da = new Process();  
  18.             //设置要启动应用程序或文档名  
  19.             da.StartInfo.FileName = @"C:\WINDOWS\system32\sdelete.exe";  
  20.             //设置要启动应用程序使用的一组命令行参数   
  21.             //参数:执行覆盖操作次数 删除文件路径  
  22.             da.StartInfo.Arguments = string.Format("-p {0} -q \"{1}\"",  
  23.                 Convert.ToInt32(textBox2.Text), textBox1.Text.ToString());  
  24.             //设置窗口状态 隐藏窗口显示  
  25.             da.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;  
  26.             //直接执行程序 不使用Shell打开  
  27.             da.StartInfo.UseShellExecute = false;                     
  28.             //输入流 输出流 错误写入流  
  29.             da.StartInfo.RedirectStandardInput = true;  
  30.             da.StartInfo.RedirectStandardOutput = true;  
  31.             da.StartInfo.RedirectStandardError = true;  
  32.             //不创建窗口,静默模式  
  33.             da.StartInfo.CreateNoWindow = true;  
  34.             //开始执行进程  
  35.             da.Start();  
  36.             //从输出流中读取程序输出  
  37.             string output = da.StandardOutput.ReadToEnd();  
  38.             //等待程序退出  
  39.             da.WaitForExit();  
  40.   
  41.             //文件不存在记录输出流信息  
  42.             if(File.Exists(textBox1.Text.ToString()))  
  43.             {  
  44.                 richTextBox1.AppendText(output);  
  45.             }  
  46.         }  
  47.     }  
  48.     catch (Exception msg) //异常处理  
  49.     {  
  50.         MessageBox.Show(msg.Message);  
  51.     }  
  52. }  

但是,虽然已经把sdelete.exe放置于“C:\windows\system32”文件夹下,使用cmd运行才能正确删除,但当通过C#代码调用时总是会显示错误"系统找不到指定文件".这让我万分伤心啊,但同时该方法的不足之处是需要用户放置sdelete程序,因此我需要寻求新的方法实现粉碎文件.

但是代码中所涉及的使用Process的方法还是值得大家学习的,我们还可以通过它实现很多功能,如下:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. //IE浏览器打开百度  
  2. Process.Start("IExplore.exe", "http://www.baidu.com/");  
  3. //打开库  
  4. Process.Start("explorer.exe");  
  5. //打开Excel办公软件  
  6. Process.Start("EXCEL.exe");  
  7. //打开cmd  
  8. Process.Start("cmd.exe");  

关于Process的详细用法建议大家阅读下面这篇优秀文章:http://blog.csdn.net/chen_zw/article/details/7896264
下面是调用cmd.exe程序实现ipconfig查看功能,但是当使用"sdelete -p 2 "F:\test.txt""时还是不能运行,我也不知道为什么?不知道怎么访问Sdelete.exe程序,使用管理员权限运行也不行.

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. string command = "";  
  2. System.Diagnostics.Process p = new System.Diagnostics.Process();  
  3. p.StartInfo.FileName = "cmd.exe";             //文件名  
  4. command = "/c" + "ipconfig";  
  5. //command = "/c" + "sdelete -p 3 \"F:\\bbb.txt\"";        
  6. p.StartInfo.Arguments = command;              //参数  
  7. p.StartInfo.UseShellExecute = false;          //是否使用操作系统shell启动  
  8. p.StartInfo.RedirectStandardInput = true;     //接受来自调用程序的输入信息  
  9. p.StartInfo.RedirectStandardOutput = true;    //由调用程序获取输出信息  
  10. p.StartInfo.RedirectStandardError = true;     //重定向标准错误输出  
  11. p.StartInfo.CreateNoWindow = true;            //不显示程序窗口  
  12. p.Start();                                    //启动程序  
  13. string output = p.StandardOutput.ReadToEnd();  
  14. richTextBox1.AppendText(output);  

cmd中ipconfig的运行结果如下,但使用sdelete参数就是不行(>.<):

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-20 08:46  applekingghfhfhbr  阅读(224)  评论(0编辑  收藏  举报