1. 程式人生 > >C#初學者使用file.creat()建立檔案後,顯示正由另一程序使用

C#初學者使用file.creat()建立檔案後,顯示正由另一程序使用

string sourcePhotoPath = this.GetUserSelectedPhoto();
            if(sourcePhotoPath == null)
            {
                return;
            }
            string sourceFileName = System.IO.Path.GetFileName(sourcePhotoPath);
            //把圖片儲存到資料夾
            string userName = this.LoginUserName;
            string newPath = @"Images";
            if(!System.IO.Directory.Exists(newPath))
            {
                System.IO.Directory.CreateDirectory(newPath);
            }
            string destFile = userName + DateTime.Now.ToString("yyyyMMddHHMMss") + "_" + sourceFileName;
            string destImgPath = System.IO.Path.Combine(newPath, destFile);
            if(!File.Exists(destImgPath))
            {
                var myFile = File.Create(destImgPath);
                myFile.Close();
            }
            FileInfo f1 = new FileInfo(sourcePhotoPath);
            f1.CopyTo(destImgPath,true);