C#初學者使用file.creat()創建文件后,顯示正由另一進程使用


從一個目錄選擇一個文件,復制到另一個目錄

 1 string sourcePhotoPath = this.GetUserSelectedPhoto();
 2             if(sourcePhotoPath == null)
 3             {
 4                 return;
 5             }
 6             string sourceFileName = System.IO.Path.GetFileName(sourcePhotoPath);
 7             //把圖片保存到文件夾
 8             string userName = this.LoginUserName;
 9             string newPath = @"Images";
10             if(!System.IO.Directory.Exists(newPath))
11             {
12                 System.IO.Directory.CreateDirectory(newPath);
13             }
14             string destFile = userName + DateTime.Now.ToString("yyyyMMddHHMMss") + "_" + sourceFileName;
15             string destImgPath = System.IO.Path.Combine(newPath, destFile);
16             if(!File.Exists(destImgPath))
17             {
18                 var myFile = File.Create(destImgPath);
19                 myFile.Close();
20             }
21             FileInfo f1 = new FileInfo(sourcePhotoPath);
22             f1.CopyTo(destImgPath,true);

 


免責聲明!

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



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