做练习时写了一个调用DOS命令删文件夹及子目录和文件的C#程序

来源:互联网 发布:错生网络剧百度云盘 编辑:程序博客网 时间:2024/05/16 14:59

  public void delFIlesandDir(string path,string dirname)
        {
            DirectoryInfo dinfo = new DirectoryInfo(path);
           


            try
            {
                foreach (DirectoryInfo d in dinfo.GetDirectories())
                {
                    if (d.Name == dirname)
                    {
                         Process p = new Process();
                        p.StartInfo.FileName = "cmd.exe";
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardInput = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                        p.StandardInput.WriteLine("attrib *.* -r -h -a -s /S /D");
                       
                        p.StandardInput.WriteLine(@"rd "+@d.FullName+" /s /q");
                       // p.WaitForExit(3000);
                        p.StandardInput.WriteLine("exit");
                      
                       
                        p.Close();
                    }else if (d.GetDirectories().Length > 0)
                    {
                        delFIlesandDir(d.FullName,dirname);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        } 

原创粉丝点击