2016-04-30 66 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); 
      } 
     } 
    } 
相关问题