[英]Mailkit. What is the best way to move an existing folder with all it's subfolders and messages to another?
[英]What is the best way to move a folder inside itself?
假設我要移動一個文件夾,而以前的內容為空,但對於新文件夾,我將如何最好地使用代碼進行處理。 通常,在資源管理器中,您可以通過剪切文件夾的內容並將其放置在原始文件夾內創建的新文件夾中來進行此操作。
即
原始路徑:C:\\ Users \\ Previous
新路徑:C:\\ Users \\ Previous \\ Previous
由於要將文件夾及其內容移動到同名子文件夾中,可以執行以下操作:
private void button1_Click(object sender, EventArgs e)
{
CopyContentsToSubFolderWithSameName(@"C:\Users\Previous");
}
private void CopyContentsToSubFolderWithSameName(string path)
{
DirectoryInfo currDir = new DirectoryInfo(path);
DirectoryInfo subDir =
Directory.CreateDirectory(Path.Combine(currDir.FullName, currDir.Name));
IEnumerable<DirectoryInfo> parentFolders =
subDir.Parent.EnumerateDirectories();
// Copy files in the current directory to the destination directory
foreach (FileInfo file in currDir.GetFiles())
{
file.MoveTo(Path.Combine(subDir.FullName, file.Name));
}
// Copy directories (including files) in the current directory
// to the destination directory
foreach (DirectoryInfo dir in parentFolders)
{
if (dir.Name != subDir.Name)
{
dir.MoveTo(Path.Combine(subDir.FullName, dir.Name));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.