c#中的文件操作

 
        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();
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值