private void btnLoadFiles_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; ofd.Filter = "视频文件(*.avi,*.mkv,*.rmvb,*.mp4,*.wmv)|*.avi;*.mkv;*.rmvb;*.mp4;*.wmv"; if (ofd.ShowDialog() == DialogResult.OK) { progressBar.Value = 0; btnStart.Enabled = false; lblTip.Text = "正在载入文件,请稍后..."; lstFiles = new List <FilesInfo>(); string[] files = ofd.FileNames; foreach (var item in files) { FilesInfo fi = new FilesInfo(); var fileName = Path.GetFileName(item); fi.FileName = fileName; fi.FileOldMd5 = ""; fi.FileNewMd5 = ""; fi.DirectoryName = Path.GetDirectoryName(item); fi.FilePath = item; lstFiles.Add(fi); } BindDgv(); btnLoadFiles.Enabled = false; tLoadFileMd5 = new Thread(CalcFilesMD5); tLoadFileMd5.Start(); } }
private bool ExecCmd(string filePath, string fileDirectory, FilesInfo fi, int filesCount, int finishedCount) { var process = new Process();//创建一个进程 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; //设定执行cmd命令 startInfo.UseShellExecute = false; //不使用系统外壳程序启动 startInfo.RedirectStandardInput = true; //不重定向输入 startInfo.RedirectStandardOutput = true; //重定向输出 startInfo.CreateNoWindow = true; //不创建窗口 process.StartInfo = startInfo; try { if (process.Start())//开始进程 { var fileNewDirectory = Path.Combine(fileDirectory, DirectoryName); var newFileDirectory = Path.Combine(fileNewDirectory, Path.GetFileName(filePath)); if (!Directory.Exists(fileNewDirectory)) { Directory.CreateDirectory(fileNewDirectory); } if (File.Exists(Path.Combine(fileNewDirectory, TempTextName + ".txt"))) { File.Delete(Path.Combine(fileNewDirectory, TempTextName + ".txt")); } if (File.Exists(newFileDirectory)) { File.Delete(newFileDirectory); } process.StandardInput.WriteLine("copy nul+nul " + Path.Combine(fileNewDirectory, TempTextName + ".txt")); process.StandardInput.WriteLine("copy /b " + filePath + "+" + fileNewDirectory + "\\" + TempTextName + ".txt " + newFileDirectory); process.StandardInput.WriteLine("exit"); process.WaitForExit(); //这里无限等待进程结束 var output = process.StandardOutput.ReadToEnd(); //读取进程的输出 if (output.IndexOf("命令语法不正确") != -1) { MessageBox.Show("请确保所有的文件夹中或文件名不含有中文或空格!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); Directory.Delete(fileNewDirectory); return(false); } else { MessageBox.Show(Path.Combine(fileNewDirectory, Path.GetFileName(filePath))); fi.FileNewMd5 = GetFileMd5(Path.Combine(fileNewDirectory, Path.GetFileName(filePath))); if (File.Exists(fileNewDirectory + TempTextName + ".txt")) { File.Delete(fileNewDirectory + TempTextName + ".txt"); } dgvFilesInfo.BeginInvoke(new Action(delegate { BindDgv(); progressBar.Value = (int)(finishedCount * 100 / filesCount * 100) / 100; })); } } } catch { MessageBox.Show("修改MD5出错,请重试!"); process.Close(); return(false); } finally { if (process != null) { process.Close(); } } return(true); }