PDA

View Full Version : آیا می توان یک دایرکتوری را با کل محتوای آن کپی کرد؟



Emad499
پنج شنبه 02 اسفند 1386, 11:46 صبح
آیا می توان یک دایرکتوری را با فایلهای داخل آن کپی کرد؟

raravaice
پنج شنبه 02 اسفند 1386, 12:19 عصر
میشه ولی دات نت چنین کلاسی رو نداره خودت باید یه سری فن بزنی مثال زیر با vb هست از کلیات کار سر در بیار #C رو بنویس.



Public Function CopyDirectory(ByVal Src As String, ByVal Dest As String, Optional _
ByVal bQuiet As Boolean = False) As Boolean
If Not Directory.Exists(Src) Then
Throw New DirectoryNotFoundException("The directory " & Src & " does not exists")
End If
If Directory.Exists(Dest) AndAlso Not bQuiet Then
If MessageBox.Show("directory " & Dest & " already exists." & vbCrLf & _
"If you continue, any files with the same name will be overwritten", _
"Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = DialogResult.Cancel Then Exit Function
End If

'add Directory Seperator Character (\) for the string concatenation shown later
If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then
Dest += Path.DirectorySeparatorChar
End If
If Not Directory.Exists(Dest) Then Directory.CreateDirectory(Dest)
Dim Files As String()
Files = Directory.GetFileSystemEntries(Src)
Dim element As String
For Each element In Files
If Directory.Exists(element) Then
'if the current FileSystemEntry is a directory,
'call this function recursively
CopyDirectory(element, Dest & Path.GetFileName(element), True)
Else
'the current FileSystemEntry is a file so just copy it
File.Copy(element, Dest & Path.GetFileName(element), True)
End If
Next
Return True
End Function

'Example usage:
Private Sub cmdCopyDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles cmdCopyDir.Click
If CopyDirectory("D:\somePath", "C:\someOtherPath") Then
MessageBox.Show("Success copying directory")
Else
MessageBox.Show("Error copying directory")
End If
End Sub


موفق باشید

اَرژنگ
پنج شنبه 02 اسفند 1386, 15:15 عصر
میشه ولی دات نت چنین کلاسی رو نداره خودت باید یه سری فن بزنی مثال زیر با vb هست از کلیات کار سر در بیار #C رو بنویس.


http://msdn2.microsoft.com/en-us/library/cc148994.aspx
وقتی که کد سی شارپ هست چرا با ویبی کار کنند؟


http://msdn2.microsoft.com/en-us/library/cc148994.aspx
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";

// Use Path class to manipulate file and directory paths.
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();
}
}

sinpin
پنج شنبه 02 اسفند 1386, 17:22 عصر
فکر میکنم کد پست قبلی فقط فایلها را کپی کند. اگر میخواهید محتویات تمامی زیرشاخه ها نیز کپی شود باید از یک تابع بازگشتی، مانند نمونه زیر استفاده کنید :


using System.IO;
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));
}
}

اَرژنگ
پنج شنبه 02 اسفند 1386, 23:44 عصر
فکر میکنم کد پست قبلی فقط فایلها را کپی کند.
بله درست میفرمائید، در مرجعی که براش لینک گداشتم بیشتر توضیح‌داده شده بود.

alirzn
جمعه 03 اسفند 1386, 18:27 عصر
سلام
این کد برای کپی کردن تمام محتویات یک دایرکتوری به یک دایرکتوری دیگر :

string SrcPath,DestPath;
SrcPath = @"F:\";
DestPath = @"F:\MyDir";
string[] FileList = System.IO.Directory.GetFiles(SrcPath);
System.IO.Directory.CreateDirectory(DestPath);
for (int i = 0;i<FileList.Length ;i++)
{
System.IO.File.Copy(FileList[i], DestPath + @"\" + FileList[i].Replace(SrcPath,""));
}

sinpin
جمعه 03 اسفند 1386, 18:34 عصر
سلام
این کد برای کپی کردن تمام محتویات یک دایرکتوری به یک دایرکتوری دیگر :

string SrcPath,DestPath;
SrcPath = @"F:\";
DestPath = @"F:\MyDir";
string[] FileList = System.IO.Directory.GetFiles(SrcPath);
System.IO.Directory.CreateDirectory(DestPath);
for (int i = 0;i<FileList.Length ;i++)
{
System.IO.File.Copy(FileList[i], DestPath + @"\" + FileList[i].Replace(SrcPath,""));
}
اینطور به نظر نمیرسه!
کد شما توی یه حلقه فایلها رو کپی میکنه، اما زیر فولدرها و محتویات اونها کپی نمیشن. در پستهای قبلی پاسخ صحیح داده شد.

اَرژنگ
جمعه 03 اسفند 1386, 19:40 عصر
اینطور به نظر نمیرسه!
کد شما توی یه حلقه فایلها رو کپی میکنه، اما زیر فولدرها و محتویات اونها کپی نمیشن. در پست شماره #2 و #4 پاسخ صحیح داده شد.
در لینکی که در پست شماره ۳ فرستاده شده بود جواب کامل وجود داشت!