2016-04-30 3 views
1

Ich habe die Übertragung von Bildern von „source/Bilder“ den „Destination/Fotos“ in asp.net C#. Alle Bilder im Quellordner sollten in das Ziel kopiert werden, wobei der ursprüngliche Bildname mit dem neu erzeugten Namen umbenannt wird. Wenn beispielsweise eine Datei im Quellordner mountain.jpg lautet und dieser Bildname mit dem Zielordner abgeglichen wird, muss er in Current DateTime umbenannt werden, gefolgt von einem Unterstrich und einem ursprünglichen Dateinamen (2016-05-20_mountain.jpg).Kopieren Sie alle Dateien aus Quellordner in den Zielordner alle Dateien umbenennen, während in asp.net C# Bewältigungs

ist mein Code wie folgt:

 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); 
     } 

Above Code erfolgreich kopiert alle Dateien Pfad mit demselben Namen wie ursprüngliche Namen zum Ziel, aber ich will jede Datei in anderen Namen benennen, während der Dateinamen zum Ziel übertragen .

+0

Was ist Ihre eigentliche Frage hier? "Ich habe seit langem versucht, aber nicht zum Erfolg." sagt nichts über dein Problem. – Claies

Antwort

3

Ich habe Ihren Code ändern, wie folowing:

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); 
    } 

oder Sie können auch verwenden:

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); 
    } 

Sie können das Format Datetime ändern, wie Sie möchten.

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); 
      } 
     } 
    } 
Verwandte Themen