您的位置:首页 > 编程语言 > C#

C# 文件迁移

2019-05-20 11:34 169 查看

 /// <summary>
        /// 图片文件迁移
        /// </summary>
        private void MoveFile(string Frompath, string directoryPath)
        {
            string[] picList = Directory.GetFiles(Frompath, "*.jpg"); //图片
            string[] txtList = Directory.GetFiles(Frompath, "*.txt"); //文本文件
            string[] pdfList = Directory.GetFiles(Frompath, "*.pdf");  //PDF文件
            foreach (string f in picList)
            {
                //取得文件名.
                string fName = f.Substring(Frompath.Length + 1);
                File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName), true);
            }
            foreach (string f in txtList)
            {
                string fName = f.Substring(Frompath.Length + 1);
                try
                {
                    File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName));
                }
                // 捕捉异常.
                catch (IOException copyError)
                {
                    MessageBox.Show(copyError.Message);
                }
            }
            foreach (string f in pdfList)
            {
                string fName = f.Substring(Frompath.Length + 1);
                try
                {
                    File.Copy(System.IO.Path.Combine(Frompath, fName), System.IO.Path.Combine(directoryPath, fName));
                }
                catch (IOException copyError)
                {
                    MessageBox.Show(copyError.Message);
                    return;
                }
            }
            //删除原始文件夹里的文件
            foreach (string f in txtList)
            {
                File.Delete(f);
            }
            foreach (string f in picList)
            {
                File.Delete(f);
            }
            foreach (string f in pdfList)
            {
                File.Delete(f);
            }
        }

转载:https://www.cnblogs.com/Alisa-study/p/5803597.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: