<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C# Copy Folder Recursively</title>
	<atom:link href="http://www.csharp411.com/c-copy-folder-recursively/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharp411.com/c-copy-folder-recursively/</link>
	<description>C# Development</description>
	<lastBuildDate>Fri, 03 Feb 2012 11:14:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Dark</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-18357</link>
		<dc:creator>Dark</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:19:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-18357</guid>
		<description>Aarkay, that is true.  But this code is helpful when one wants a more custom method, such as in my case.  I need to copy using a filter.  I only want to copy, let&#039;s say, *.doc files.  Now I can use this method as a starting point to create my method.</description>
		<content:encoded><![CDATA[<p>Aarkay, that is true.  But this code is helpful when one wants a more custom method, such as in my case.  I need to copy using a filter.  I only want to copy, let&#8217;s say, *.doc files.  Now I can use this method as a starting point to create my method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aarkay</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-782</link>
		<dc:creator>aarkay</dc:creator>
		<pubDate>Tue, 11 Oct 2011 05:20:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-782</guid>
		<description>Why take all this pain?

Simply use a VB function: Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory

Add a reference of Microsoft.VisualBasic to your .Net assembly. MS does not stop us from doing so.

Hope this helps somebody out there.</description>
		<content:encoded><![CDATA[<p>Why take all this pain?</p>
<p>Simply use a VB function: Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory</p>
<p>Add a reference of Microsoft.VisualBasic to your .Net assembly. MS does not stop us from doing so.</p>
<p>Hope this helps somebody out there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sri</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-781</link>
		<dc:creator>sri</dc:creator>
		<pubDate>Mon, 10 Oct 2011 20:03:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-781</guid>
		<description>It was very helpful to me..Thanks alot...</description>
		<content:encoded><![CDATA[<p>It was very helpful to me..Thanks alot&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prema</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-780</link>
		<dc:creator>Prema</dc:creator>
		<pubDate>Tue, 23 Aug 2011 03:53:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-780</guid>
		<description>protected void fnCopyDirFile(string SourcePath, string DestinPath)
        {
            try
            {
            top:
                DirectoryInfo SourceFile = new DirectoryInfo(SourcePath);
                DirectoryInfo DestinFile = new DirectoryInfo(DestinPath);


                if (!File.Exists(DestinPath))
                {
                    Directory.CreateDirectory(DestinPath);
                }

                DirectoryInfo[] SrcFle = SourceFile.GetDirectories();
                DirectoryInfo[] DstFle = DestinFile.GetDirectories();

                List SrcLst = new List();
                List DstLst = new List();

                foreach (DirectoryInfo d1 in SrcFle)
                {
                    SrcLst.Add(d1);
                }
                foreach (DirectoryInfo d2 in DstFle)
                {
                    DstLst.Add(d2);
                }

                int cnt;
                cnt = 0;
                for (int a = 0; a &lt; SrcFle.Length; a++)
                {
                    if (DstFle.Length == 0)
                    {
                        Directory.CreateDirectory(DestinPath + SrcFle[a].Name);
                        goto top;
                    }
                    cnt = 0;
                    for (int b = 0; b &lt; DstFle.Length; b++)
                    {
                        if (SrcLst[a].Name == DstLst[b].Name)
                        {
                            cnt++;
                        }
                    }
                    if (cnt == 0)
                    {
                        Directory.CreateDirectory(DestinPath + SrcLst[a].Name);

                        StreamWriter sw;
                        sw = new StreamWriter(@&quot;c:LockFile@Folder&quot; + System.DateTime.Now.ToString(&quot;dd-MMM-yyyy&quot;) + &quot;.txt&quot;, true);
                        sw.WriteLine(z + &quot;   &quot; + &quot; Folder Created:&quot; + &quot;   &quot; + SrcLst[a].Name);
                        sw.WriteLine();
                        sw.Close();
                        z++;
                    }
                    fnCopyDirFile(SourcePath + SrcLst[a].Name + @&quot;&quot;, DestinPath + SrcLst[a].Name + @&quot;&quot;);
                }
              }
            catch (Exception ex)
            {
                Console.Write(ex.Message.ToString());
            }
        }</description>
		<content:encoded><![CDATA[<p>protected void fnCopyDirFile(string SourcePath, string DestinPath)<br />
        {<br />
            try<br />
            {<br />
            top:<br />
                DirectoryInfo SourceFile = new DirectoryInfo(SourcePath);<br />
                DirectoryInfo DestinFile = new DirectoryInfo(DestinPath);</p>
<p>                if (!File.Exists(DestinPath))<br />
                {<br />
                    Directory.CreateDirectory(DestinPath);<br />
                }</p>
<p>                DirectoryInfo[] SrcFle = SourceFile.GetDirectories();<br />
                DirectoryInfo[] DstFle = DestinFile.GetDirectories();</p>
<p>                List SrcLst = new List();<br />
                List DstLst = new List();</p>
<p>                foreach (DirectoryInfo d1 in SrcFle)<br />
                {<br />
                    SrcLst.Add(d1);<br />
                }<br />
                foreach (DirectoryInfo d2 in DstFle)<br />
                {<br />
                    DstLst.Add(d2);<br />
                }</p>
<p>                int cnt;<br />
                cnt = 0;<br />
                for (int a = 0; a &lt; SrcFle.Length; a++)<br />
                {<br />
                    if (DstFle.Length == 0)<br />
                    {<br />
                        Directory.CreateDirectory(DestinPath + SrcFle[a].Name);<br />
                        goto top;<br />
                    }<br />
                    cnt = 0;<br />
                    for (int b = 0; b &lt; DstFle.Length; b++)<br />
                    {<br />
                        if (SrcLst[a].Name == DstLst[b].Name)<br />
                        {<br />
                            cnt++;<br />
                        }<br />
                    }<br />
                    if (cnt == 0)<br />
                    {<br />
                        Directory.CreateDirectory(DestinPath + SrcLst[a].Name);</p>
<p>                        StreamWriter sw;<br />
                        sw = new StreamWriter(@&quot;c:LockFile@Folder&quot; + System.DateTime.Now.ToString(&quot;dd-MMM-yyyy&quot;) + &quot;.txt&quot;, true);<br />
                        sw.WriteLine(z + &quot;   &quot; + &quot; Folder Created:&quot; + &quot;   &quot; + SrcLst[a].Name);<br />
                        sw.WriteLine();<br />
                        sw.Close();<br />
                        z++;<br />
                    }<br />
                    fnCopyDirFile(SourcePath + SrcLst[a].Name + @&quot;&quot;, DestinPath + SrcLst[a].Name + @&quot;&quot;);<br />
                }<br />
              }<br />
            catch (Exception ex)<br />
            {<br />
                Console.Write(ex.Message.ToString());<br />
            }<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aswath</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-779</link>
		<dc:creator>Aswath</dc:creator>
		<pubDate>Fri, 08 Jul 2011 19:32:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-779</guid>
		<description>Works like a charm. Thanks!</description>
		<content:encoded><![CDATA[<p>Works like a charm. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dhinesh</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-778</link>
		<dc:creator>Dhinesh</dc:creator>
		<pubDate>Wed, 30 Mar 2011 07:30:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-778</guid>
		<description>Thanks Eitan. 

I tried the Parallel framework in .NET 4 and updated my code is here. It will give better performance.

      private void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
        {
            foreach (DirectoryInfo dir in source.GetDirectories())
            {
                DirectoryInfo targetSubDirectory = target.CreateSubdirectory(dir.Name);
                CopyFilesRecursively(dir, targetSubDirectory);
            }

            Parallel.ForEach(source.GetFiles(),
            filePath =&gt;
            {
                System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Factory.StartNew(() =&gt;
                {
                    filePath.CopyTo(Path.Combine(target.FullName, filePath.Name));
                });
            });
        }</description>
		<content:encoded><![CDATA[<p>Thanks Eitan. </p>
<p>I tried the Parallel framework in .NET 4 and updated my code is here. It will give better performance.</p>
<p>      private void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)<br />
        {<br />
            foreach (DirectoryInfo dir in source.GetDirectories())<br />
            {<br />
                DirectoryInfo targetSubDirectory = target.CreateSubdirectory(dir.Name);<br />
                CopyFilesRecursively(dir, targetSubDirectory);<br />
            }</p>
<p>            Parallel.ForEach(source.GetFiles(),<br />
            filePath =&gt;<br />
            {<br />
                System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Factory.StartNew(() =&gt;<br />
                {<br />
                    filePath.CopyTo(Path.Combine(target.FullName, filePath.Name));<br />
                });<br />
            });<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eitan</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-777</link>
		<dc:creator>Eitan</dc:creator>
		<pubDate>Tue, 29 Mar 2011 19:52:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-777</guid>
		<description>Here is an improved version, which copies also empty folders:

public static void CopyAllFiles(string sourceFolder, string destinationFolder)
        {
            foreach (string directory in Directory.GetDirectories(sourceFolder, &quot;*&quot;, SearchOption.AllDirectories))
            {
                string DestinationFilePath = String.Format(&quot;{0}{1}{2}&quot;, 
                    destinationFolder, Path.DirectorySeparatorChar, directory.Replace(sourceFolder, &quot;&quot;));
                
                if (!Directory.Exists(DestinationFilePath))
                    Directory.CreateDirectory(DestinationFilePath);

                foreach (string file in Directory.GetFiles(directory, &quot;*&quot;, SearchOption.TopDirectoryOnly))
                {
                    File.Copy(file, Path.Combine(DestinationFilePath, Path.GetFileName(file)), true);
                }
            }
        }

I don&#039;t think that there are performance differences between the recursive or iterative versions, the bottleneck is the copy itself. I just don&#039;t like recursion. In order to lower copy times, you will need 1) go to a lower level (maybe file streams at byte level), and/or 2) multi-threaded copy (the new parallel framework in .NET 4 might help here, although I haven&#039;t tried it, yet)</description>
		<content:encoded><![CDATA[<p>Here is an improved version, which copies also empty folders:</p>
<p>public static void CopyAllFiles(string sourceFolder, string destinationFolder)<br />
        {<br />
            foreach (string directory in Directory.GetDirectories(sourceFolder, &#8220;*&#8221;, SearchOption.AllDirectories))<br />
            {<br />
                string DestinationFilePath = String.Format(&#8220;{0}{1}{2}&#8221;,<br />
                    destinationFolder, Path.DirectorySeparatorChar, directory.Replace(sourceFolder, &#8220;&#8221;));</p>
<p>                if (!Directory.Exists(DestinationFilePath))<br />
                    Directory.CreateDirectory(DestinationFilePath);</p>
<p>                foreach (string file in Directory.GetFiles(directory, &#8220;*&#8221;, SearchOption.TopDirectoryOnly))<br />
                {<br />
                    File.Copy(file, Path.Combine(DestinationFilePath, Path.GetFileName(file)), true);<br />
                }<br />
            }<br />
        }</p>
<p>I don&#8217;t think that there are performance differences between the recursive or iterative versions, the bottleneck is the copy itself. I just don&#8217;t like recursion. In order to lower copy times, you will need 1) go to a lower level (maybe file streams at byte level), and/or 2) multi-threaded copy (the new parallel framework in .NET 4 might help here, although I haven&#8217;t tried it, yet)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dhinesh</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-776</link>
		<dc:creator>Dhinesh</dc:creator>
		<pubDate>Tue, 29 Mar 2011 15:33:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-776</guid>
		<description>Hi Eitan, 

I tried your code. Its fail when i have empty folder inside the sub directory. otherwise I didn&#039;t see any performance improvements</description>
		<content:encoded><![CDATA[<p>Hi Eitan, </p>
<p>I tried your code. Its fail when i have empty folder inside the sub directory. otherwise I didn&#8217;t see any performance improvements</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Milo</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-775</link>
		<dc:creator>Milo</dc:creator>
		<pubDate>Wed, 09 Feb 2011 20:52:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-775</guid>
		<description>This is refactored safe version

http://01792.org/blog/post/2010/10/06/Recursive-file-and-folder-copy.aspx

In 4 lines</description>
		<content:encoded><![CDATA[<p>This is refactored safe version</p>
<p><a href="http://01792.org/blog/post/2010/10/06/Recursive-file-and-folder-copy.aspx" rel="nofollow">http://01792.org/blog/post/2010/10/06/Recursive-file-and-folder-copy.aspx</a></p>
<p>In 4 lines</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andre Bertonha Martins</title>
		<link>http://www.csharp411.com/c-copy-folder-recursively/#comment-774</link>
		<dc:creator>Andre Bertonha Martins</dc:creator>
		<pubDate>Fri, 12 Nov 2010 11:25:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/c-copy-folder-recursively/#comment-774</guid>
		<description>yeah
it´s just this
it works very well
thanks man</description>
		<content:encoded><![CDATA[<p>yeah<br />
it´s just this<br />
it works very well<br />
thanks man</p>
]]></content:encoded>
	</item>
</channel>
</rss>

