Recursive copy utility and file I/O permission

Posted: July 31, 2005 in Uncategorized
I got a query that how to recursively copy all the folders & files given a path to the source and destination folders.It was also asked that since there is no folder.copy command in the .net framework how to accomplish the same.Here is the solution I have craeted for the same have a look:-
 
 
below is the example code and much simplified where i am considering the ReadOnly..you can add ur Hidden and other attributes accordingly:-
 

private void button2_Click(object sender, System.EventArgs e)

{

copyfolder(@”C:\Documents and Settings\shreeman\Desktop\temp read and delete”,@”C:\Documents and Settings\shreeman\Desktop\new temp read and delete”);

MessageBox.Show(“copy complete”);

 

}

 

privatevoid copyfolder(string sourcedir,string destdir)

{

 

DirectoryInfo dirinfo=

new DirectoryInfo(sourcedir);

 

if(Directory.Exists(destdir)==false)

{

Directory.CreateDirectory(destdir);

}

 

foreach(FileSystemInfo fsinf in dirinfo.GetFileSystemInfos())

{

 

string destfilename=Path.Combine(destdir,fsinf.Name);

 

if ( fsinf is FileInfo)

{

 

if (File.Exists(destfilename)==true)

{

if ((File.GetAttributes(destfilename) != FileAttributes.Normal))

{

File.SetAttributes(destfilename,FileAttributes.Normal);

}

}

// Try to copy the file.

File.Copy(fsinf.FullName,destfilename,

true);

 

}

else

{

copyfolder(fsinf.FullName,destfilename);

}

}

 

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s