Jump to content

probleme de deplacement de repertoire


zaki
 Share

Recommended Posts

bonjour les amis

svp comment faire deplacer un repertoire entre 2 volumes différentes (du D: vers C:)

 

j'ai essayé de la faire avec directory.move mais j'ai eu une exeption >

 

est ce qu'il y a une autre méthode ?

 

merci

Link to comment
Share on other sites

The exception is clear you cannot move Directories across volumes.

Try this solution:


public void MoveDirectory(string sourceDir, string distDir, bool delSource)
{
if(Directory.Exists(sourceDir)
{
if(Directory.GetDirectoryRoot(sourceDir) == Directory.GetDirectoryRoot(destDir)
{
Directory.Move(sourceDir, destDir);
}
else
{
CopyDirectory(new DirectoryInfo(sourceDir), new DirectoryInfo(destDir));
if(delSource)
{
Directory.Delete(sourceDir, true);
}
}
}
}

private void CopyDirectory(DirectoryInfo sourceDir, DirectoryInfo destDir)
{
if(!destDir.Exists)
{
destDir.Create();
}
FileInfo[] srcFiles = sourceDir.GetFiles();
foreach(FileInfo srcFile in srcFiles)
{
srcFile.CopyTo(Path.Combine(destDir.FullName, srcFile.Name));
}
DirectoryInfo[] scrDirectories = sourceDir.GetDirectories();
foreach(DirectoryInfo scrDirectory in srcDirectories)
{
CopyDirectory(srcDirectory, new DirectoryInfo(Path.Combine(destDir.FulleName,
srcDirectory.Name)));
}
}
[/Code]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...