2016-04-30 64 views
1

我不得不從「SourceFolder /圖片」圖像傳輸到asp.net「DestinationFolder /照片」 c#。應將源文件夾中的所有圖像複製到使用新生成的名稱重命名原始圖像名稱的目標。例如,如果源文件夾中的文件爲mountain.jpg,並且將該圖像名稱複製到目標文件夾,則需要將其重命名爲當前日期時間,後跟下劃線和原始文件名(2016-05-20_mountain.jpg)。複製從源文件夾複製到目標文件夾中的所有文件重命名的所有文件,而在asp.net C#應對

我的代碼如下:

 string sourcePath = Server.MapPath("~/SourceFolder/Images"); 
     string targetPath = Server.MapPath("~/DestinationFolder/Photos");   
     foreach (var srcPath in Directory.GetFiles(sourcePath)) 
     {     
      File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true); 
     } 

以上代碼成功地將所有文件複製到目標具有相同的名稱,原來的名稱路徑,但我想在傳送文件名到目的地對每個文件重命名爲不同的名字。

+0

這裏你的實際問題是什麼? 「我嘗試了很久,但無法取得成功。」並沒有提到你的問題。 – Claies

回答

3

我不得不改變你的代碼如下因素:

string sourcePath = Server.MapPath("~/SourceFolder/Images"); 
    string targetPath = Server.MapPath("~/DestinationFolder/Photos"); 

    foreach (var srcPath in Directory.GetFiles(sourcePath)) 
    { 

     FileInfo fileInfo = new FileInfo(srcPath); 
     string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + fileInfo.Name; 

     File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath).Replace(fileInfo.Name, fileName), true); 
    } 

,或者您也可以使用:

string sourcePath = Server.MapPath("~/SourceFolder/Images"); 
    string targetPath = Server.MapPath("~/DestinationFolder/Photos"); 

    foreach (var srcPath in Directory.GetFiles(sourcePath)) 
    { 
      FileInfo fileInfo = new FileInfo(srcPath); 
      string fileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), fileInfo.Name); 

      File.Copy(srcPath, string.Format("{0}/{1}", targetPath, fileName), true); 
    } 

,只要你喜歡,你可以改變DateTime格式。

1
static void CopytoDestination(string sourcePath,string sourcePath) 
    { 
     string fileName = "test.txt"; 

     string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 
     string destFile = System.IO.Path.Combine(targetPath, fileName); 

     // To copy a folder's contents to a new location: 
     // Create a new target folder, if necessary. 
     if (!System.IO.Directory.Exists(targetPath)) 
     { 
      System.IO.Directory.CreateDirectory(targetPath); 
     } 

     // To copy a file to another location and 
     // overwrite the destination file if it already exists. 
     System.IO.File.Copy(sourceFile, destFile, true); 

     // To copy all the files in one directory to another directory. 
     // Get the files in the source folder. (To recursively iterate through 
     // all subfolders under the current directory, see 
     // "How to: Iterate Through a Directory Tree.") 
     // Note: Check for target path was performed previously 
     //  in this code example. 
     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 

      // Copy the files and overwrite destination files if they already exist. 
      foreach (string s in files) 
      { 
       // Use static Path methods to extract only the file name from the path. 
       fileName = System.IO.Path.GetFileName(s); 
       destFile = System.IO.Path.Combine(targetPath, fileName); 
       System.IO.File.Copy(s, destFile, true); 
      } 
     } 
     else 
     { 
      Console.WriteLine("Source path does not exist!"); 
     } 

     // Keep console window open in debug mode. 
     Console.WriteLine("Press any key to exit."); 
     Console.ReadKey(); 
    } 
0
protected void btnTransferFiles_Click(object sender, EventArgs e) 
    { 
     string sourcePath = Server.MapPath("~/SourceFolder/Images"); 
     string targetPath = Server.MapPath("~/DestinationFolder/Photos"); 

     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 
      string fileName = string.Empty; 
      string destFile = string.Empty; 

      // Copy the files and overwrite destination files if they already exist. 
      foreach (string s in files) 
      { 
       // Use static Path methods to extract only the file name from the path. 
       fileName = System.IO.Path.GetFileName(s); 

       destFile = System.IO.Path.Combine(targetPath, DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + fileName); 
       System.IO.File.Copy(s, destFile, true); 
      } 
     } 
    } 
相關問題