2009-03-24 10 views
14

का उपयोग कर रहा फ़ोल्डर सी कॉपी करने की जरूरत है: \ FromFolder सेल्सियस के लिए: \ ToFolderकॉपी फ़ोल्डर System.IO

नीचे कोड है कि मेरे FromFolder कट जाएगा और फिर मेरी ToFolder बनाएगा है। तो मेरी FromFolder चला जाएगा और सभी आइटम ToFolder

System.IO.Directory.Move(@"C:\FromFolder ", @"C:\ToFolder"); 

बुलाया नव निर्मित फ़ोल्डर में हो जाएगा लेकिन मैं सिर्फ ToFolder को FromFolder में फाइल कॉपी करना चाहते हैं। किसी कारण से कोई System.IO.Directory.Copy ???

यह कैसे एक बैच फ़ाइल का उपयोग किया जाता - बहुत आसान

xcopy C: \ FromFolder C: \ ToFolder

सादर एटीन

उत्तर

17

यह लिंक एक अच्छा उदाहरण प्रदान करता है।

http://msdn.microsoft.com/en-us/library/cc148994.aspx

यहाँ एक टुकड़ा

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

वहाँ एक फ़ाइल प्रतिलिपि है। विश्राम फ़ोल्डर और नया एक example

static void Main(string[] args) 
    { 
     DirectoryInfo sourceDir = new DirectoryInfo("c:\\a"); 
     DirectoryInfo destinationDir = new DirectoryInfo("c:\\b"); 

     CopyDirectory(sourceDir, destinationDir); 

    } 

    static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination) 
    { 
     if (!destination.Exists) 
     { 
      destination.Create(); 
     } 

     // Copy all files. 
     FileInfo[] files = source.GetFiles(); 
     foreach (FileInfo file in files) 
     { 
      file.CopyTo(Path.Combine(destination.FullName, 
       file.Name)); 
     } 

     // Process subdirectories. 
     DirectoryInfo[] dirs = source.GetDirectories(); 
     foreach (DirectoryInfo dir in dirs) 
     { 
      // Get destination directory. 
      string destinationDir = Path.Combine(destination.FullName, dir.Name); 

      // Call CopyDirectory() recursively. 
      CopyDirectory(dir, new DirectoryInfo(destinationDir)); 
     } 
    } 
2

करने के लिए मूल निर्देशिका से सभी फ़ाइलों की प्रतिलिपि आप से अधिक स्रोत निर्देशिका में सभी फ़ाइलों के माध्यम से स्क्रैच से एक नया निर्देशिका तो पाश बनाने और उन्हें कॉपी करना होगा।

string[] files = Directory.GetFiles(GlobalVariables.mstrReadsWellinPath); 
foreach(string s in files) 
{ 
     fileName=Path.GetFileName(s); 
     destFile = Path.Combine(DestinationPath, fileName); 
     File.Copy(s, destFile); 
} 

मैं तुम्हें करने के लिए गंतव्य निर्देशिका बनाने छोड़ :-)

+0

आसान समाधान मौजूद नहीं है अब तक! धन्यवाद! – agleno

1

This article provides an alogirthm to copy recursively some folder and all its content

लेख से:

दुर्भाग्य से वहाँ है System.IO में कोई अंतर्निहित समारोह है कि एक फ़ोल्डर और इसकी सामग्री की प्रतिलिपि बनायेगा। निम्नलिखित एक सरल रिकर्सिव एल्गोरिदम है जो एक फ़ोल्डर, इसके उप-फ़ोल्डर और फ़ाइलों की प्रतिलिपि बनाता है, यदि आवश्यक हो तो गंतव्य फ़ोल्डर बना रहा है। सादगी के लिए, कोई त्रुटि प्रबंधन नहीं है; कुछ भी गलत होने पर अपवाद फेंक देगा, जैसे शून्य या अमान्य पथ या गंतव्य फाइलें पहले से मौजूद हैं।

शुभकामनाएँ!

2

आप सही हैं। कोई निर्देशिका नहीं है। कॉपी विधि। यह एक बहुत ही शक्तिशाली तरीका होगा, लेकिन यह भी एक खतरनाक व्यक्ति होगा, बिना किसी समझदार डेवलपर के लिए। एक फ़ोल्डर की प्रतिलिपि बनाना potentionaly एक बहुत समय लेने वाला ऑपरेशन हो सकता है, जबकि एक (उसी ड्राइव पर) को स्थानांतरित करना नहीं है।

मुझे लगता है कि माइक्रोसॉफ्ट ने सोचा था कि यह फ़ाइल द्वारा फ़ाइल की प्रतिलिपि बनाने के लिए सेंस बनाएगा, ताकि आप किसी प्रकार की प्रगति जानकारी दिखा सकें। आप DirectoryInfo का उदाहरण बनाकर और फिर GetFiles() को कॉल करके निर्देशिका में फ़ाइलों को गड़बड़ कर सकते हैं। उपनिर्देशिकाओं को भी शामिल करने के लिए आप GetDirectories() को भी कॉल कर सकते हैं और रिकर्सिव विधि के साथ इन्हें गड़बड़ कर सकते हैं।

5

प्रतिलिपि बनाई जा रही निर्देशिका (सही ढंग से) वास्तव में एक जटिल काम है का उपयोग कर सकते खाते उन्नत फाइल सिस्टम में खासकर यदि आप ले जंक्शन और हार्ड लिंक जैसी तकनीकें। आपकी सबसे अच्छी शर्त एपीआई का उपयोग करना है जो इसका समर्थन करती है। यदि आप थोड़ा पी/Invoke से डरते नहीं हैं, तो शेल 32 में SHFileOperation आपकी सबसे अच्छी शर्त है। माइक्रोसॉफ्ट में माइक्रोसॉफ्ट.विज़ुअल बेसिक.फाइलियो.फाइलसिस्टम.कॉपी डायरेक्टरी विधि का उपयोग करने का एक और विकल्प होगा। विज़ुअल बेसिक असेंबली (भले ही आप वीबी का उपयोग नहीं कर रहे हों)।

0

विस्तार का उपयोग कर DirectoryInfo.CopyTo का मेरा संस्करण।

public static class DirectoryInfoEx { 
    public static void CopyTo(this DirectoryInfo source, DirectoryInfo target) { 
     if (source.FullName.ToLower() == target.FullName.ToLower()) 
      return; 

     if (!target.Exists) 
      target.Create(); 

     foreach (FileInfo f in source.GetFiles()) { 
      FileInfo newFile = new FileInfo(Path.Combine(target.FullName, f.Name)); 
      f.CopyTo(newFile.FullName, true); 
     } 

     foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { 
      DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); 
      diSourceSubDir.CopyTo(nextTargetSubDir); 
     } 
    } 
} 

और उपयोग करने वाले की तरह ...

DirectoryInfo d = new DirectoryInfo("C:\Docs"); 
d.CopyTo(new DirectoryInfo("C:\New")); 
+0

यह प्रतिलिपि फ़ोल्डर संरचना में कितनी गहरी है? ऐसा लगता है कि यह केवल सबफ़ोल्डर की प्रतिलिपि करेगा - उप-सबफ़ोल्डर नहीं। ऐसा लगता है कि @RvdK समाधान केवल रिकर्सिव है। – Jag

+0

यह रिकर्सिव @ जैग है, आखिरी फोरच में निर्देशिका-जो उप-निर्देशिकाओं को इंटरैक्ट करने के लिए उपयोग की जाती है, उसी एक्सटेंशन "CopyTo" का उपयोग करती है। ; पी – diegodsp

+0

आह हाँ, मैं सही खड़ा हूँ। एक गोरा पल होना चाहिए :) – Jag

0

इस कोड को उपयोगकर्ता एक से दूसरे निर्देशिका की सामग्री कॉपी करने के लिए सक्षम बनाता है।

using System; 
using System.IO; 

// ... 
    static void Main(string[] args) 
    { 
     Console.ForegroundColor = ConsoleColor.Cyan; 
     Console.Write("Copying... "); 

     DeepCopy(@"C:\Books", @"D:\Books"); 

     Console.WriteLine("Completed"); 

     Console.ForegroundColor = ConsoleColor.Gray; 
     Console.Write("Press any key to quit . . . "); 
     Console.ReadKey(true); 
    } 

    static void DeepCopy(string destination, string source) 
    { 
     Directory.CreateDirectory(destination); 

     string[] files = Directory.GetFiles(source); 
     foreach (string filepath in files) 
     { 
      int fileindex = filepath.LastIndexOf('\\'); 
      string filename = filepath.Substring(fileindex); 

      Copy(destination + filename, filepath); 
     } 

     string[] directories = Directory.GetDirectories(source); 
     foreach (string directory in directories) 
     { 
      int subdirindex = directory.LastIndexOf('\\'); 
      string subdirname = directory.Substring(subdirindex); 
      string structure = destination + subdirname; 

      DeepCopy(structure, directory); 
     } 
    } 

    static void Copy(string destination, string source, bool overwrite = false) 
    { 
     if (overwrite == true) 
      if (File.Exists(destination)) 
       File.Delete(destination); 

     if (File.Exists(destination)) 
      return; 

     using (Stream filereader = File.OpenRead(source)) 
     using (Stream filewriter = File.OpenWrite(destination)) 
     { 
      int readlength = 0; 
      byte[] buffer = new byte[1048576]; 
      while (filereader.Length != filewriter.Length) 
      { 
       readlength = filereader.Read(buffer, 0, 1048576); 
       filewriter.Write(buffer, 0, readlength); 
      } 
     } 
    } 
2

एक साधारण समारोह है कि प्रतियां स्रोत फ़ोल्डर की संपूर्ण सामग्री गंतव्य फ़ोल्डर में और गंतव्य फ़ोल्डर बनाता है अगर यह

class Utils 
{ 
    internal static void copy_dir(string source, string dest) 
    { 
     if (String.IsNullOrEmpty(source) || String.IsNullOrEmpty(dest)) return; 
     Directory.CreateDirectory(dest); 
     foreach (string fn in Directory.GetFiles(source)) 
     { 
      File.Copy(fn, Path.Combine(dest, Path.GetFileName(fn)), true); 
     } 
     foreach (string dir_fn in Directory.GetDirectories(source)) 
     { 
      copy_dir(dir_fn, Path.Combine(dest, Path.GetFileName(dir_fn))); 
     } 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे