C# 復制文件到指定文件夾


直接使用 FileInfo.CopyTo 方法
代碼如下:

        public void saveFile(string filePathName , string toFilesPath)
        {
            FileInfo file = new FileInfo(filePathName);
            string newFileName= file.Name;
            file.CopyTo(toFilesPath + @"\" + newFileName, true);
        }

參數說明:
filePathName:將要被復制的文件的完整路徑
toFilesPath:復制到所指定的文件夾的完整路徑

調用示例:

     string filePath = @"C:\test1\hello.jpg";
     string toFilesPath = @"D:\test2";
     saveFile(filePath , toFilesPath )

方法重載:復制后改變文件名稱

        public void saveFile(string filePathName , string toFilesPath , string newFileName)
        {
            FileInfo file = new FileInfo(filePathName);
            file.CopyTo(toFilesPath + @"\" + newFileName, true);
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM