private void InitComponentContent() { //this.tbDataFile.Text = "C://test.txt"; this.tbFileExtension.Text = ".wav"; //this.tbOpenPath.Text = "D://music//纯音乐//钢琴曲//"; //this.tbSavePath.Text = "E://新建文件夹//"; } private void DataNameFormat() { string dataFileNameFormat = String.Format("{0}{1}{2}{3}{4}{5}", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString(), DateTime.Now.Hour.ToString(), DateTime.Now.Minute.ToString(), DateTime.Now.Second.ToString()); this.dataFileSaveOK = String.Format("{0}{1}成功文件列表{2}", this.tbSavePath.Text, dataFileNameFormat, this.dataFileSaveExtension); this.dataFileSaveNO = String.Format("{0}{1}失败文件列表{2}", this.tbSavePath.Text, dataFileNameFormat, this.dataFileSaveExtension); } private void btnDataFile_Click(object sender, EventArgs e) { this.ofdDataFile.ShowDialog(); this.tbDataFile.Text = this.ofdDataFile.FileName; } private void btnOpenPath_Click(object sender, EventArgs e) { this.fbdOpenPath.ShowDialog(); if (this.fbdOpenPath.SelectedPath.EndsWith("//")) this.tbOpenPath.Text = this.fbdOpenPath.SelectedPath; else this.tbOpenPath.Text = String.Format("{0}//", this.fbdOpenPath.SelectedPath); } private void btnSavePath_Click(object sender, EventArgs e) { this.fbdSavePath.ShowDialog(); if (this.fbdSavePath.SelectedPath.EndsWith("//")) this.tbSavePath.Text = this.fbdSavePath.SelectedPath; else this.tbSavePath.Text = String.Format("{0}//", this.fbdSavePath.SelectedPath); } private void btnSubmit_Click(object sender, EventArgs e) { this.DataNameFormat(); this.objReader = new StreamReader(this.tbDataFile.Text); string sLine = ""; ArrayList arrFiles = new ArrayList(); while (sLine != null) { sLine = objReader.ReadLine(); if (sLine != null) arrFiles.Add(sLine); } objReader.Close(); this.progressBar1.Minimum = 1; this.progressBar1.Maximum = arrFiles.Count; this.progressBar1.Step = 10; this.progressBar1.Value = 1; foreach (string fileName in arrFiles) { string fileNamePatch = String.Format("{0}*{1}", fileName, this.tbFileExtension.Text); string[] fileArray = System.IO.Directory.GetFiles(this.tbOpenPath.Text, fileNamePatch, System.IO.SearchOption.TopDirectoryOnly); if (fileArray.Length > 0) { //string filePathOpen = String.Format("{0}{1}{2}", this.tbOpenPath.Text, fileArray[, this.tbFileExtension.Text); for (int i = 0; i < fileArray.Length; i++) { string filePathSave = String.Format("{0}{1}", this.tbSavePath.Text, fileArray[i].Substring(fileArray[i].LastIndexOf('//'))); File.Copy(fileArray[i], filePathSave); } this.SaveData(true, fileName, fileArray.Length); } else this.SaveData(false, fileName, 0); //if (File.Exists(filePathOpen)) //{ // File.Copy(filePathOpen, filePathSave); // this.SaveData(true, fileName); //} //else // this.SaveData(false, fileName); this.progressBar1.PerformStep(); } } private void SaveData(bool SaveOK, string fileName,int fileCount) { string fileNameFormat = String.Format("编号:{0} 文件数量:{1}", fileName, fileCount.ToString()); StreamWriter sw; if (SaveOK) { if (!File.Exists(this.dataFileSaveOK)) { sw = new StreamWriter(this.dataFileSaveOK, true, System.Text.Encoding.GetEncoding("GB2312")); sw.WriteLine("处理成功的数据有:"); sw.Close(); } sw = new StreamWriter(this.dataFileSaveOK, true, System.Text.Encoding.GetEncoding("GB2312")); sw.WriteLine(fileNameFormat); sw.Close(); } else { if (!File.Exists(this.dataFileSaveNO)) { sw = new StreamWriter(this.dataFileSaveNO, true, System.Text.Encoding.GetEncoding("GB2312")); sw.WriteLine("处理失败的数据有:"); sw.Close(); } sw = new StreamWriter(this.dataFileSaveNO, true, System.Text.Encoding.GetEncoding("GB2312")); sw.WriteLine(fileNameFormat); sw.Close(); } }
c#中的文件操作
最新推荐文章于 2024-11-07 08:51:26 发布