日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現拷貝文件到另一個文件夾下_C#教程

作者:熊思宇 ? 更新時間: 2023-03-20 編程語言

C#拷貝文件到另一個文件夾下

/// <summary>
/// 拷貝文件到另一個文件夾下
/// </summary>
/// <param name="sourceName">源文件路徑</param>
/// <param name="folderPath">目標路徑(目標文件夾)</param>
public void CopyToFile(string sourceName, string folderPath)
{
    //例子:
    //源文件路徑
    //string sourceName = @"D:\Source\Test.txt";
    //目標路徑:項目下的NewTest文件夾,(如果沒有就創建該文件夾)
    //string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewTest");
 
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }
 
    //當前文件如果不用新的文件名,那么就用原文件文件名
    string fileName = Path.GetFileName(sourceName);
    //這里可以給文件換個新名字,如下:
    //string fileName = string.Format("{0}.{1}", "newFileText", "txt");
 
    //目標整體路徑
    string targetPath = Path.Combine(folderPath, fileName);
 
    //Copy到新文件下
    FileInfo file = new FileInfo(sourceName);
    if (file.Exists)
    {
        //true 為覆蓋已存在的同名文件,false 為不覆蓋
        file.CopyTo(targetPath, true);
    }
}

注意方法內注釋的用法,可以根據自己的需要更改。

C#文件搬運(從一個文件夾Copy至另一個文件夾)

時常我們會遇到文件的復制、上傳等問題。特別是自動化生產方面,需要對機臺拋出的檔案進行搬運、收集,然后對資料里的數據等進行分析,等等。

Winform下,列舉集中較常見的檔案的搬運。

private void MoveFile()
        {
            string Frompath = @"D:\Test\OutPut";
            string directoryPath = @"D:\report";
            try
            {
                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);
                }

            }
            catch (DirectoryNotFoundException dirNotFound)
            {
                MessageBox.Show(dirNotFound.Message);
            }
        }

總結

原文鏈接:https://blog.csdn.net/qq_38693757/article/details/115730668

欄目分類
最近更新