开发者

How to move file/folder to an already existing folder [closed]

开发者 https://www.devze.com 2023-03-22 05:23 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or r开发者_JAVA百科hetorical andcannot be reasonably answered in its current form.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or r开发者_JAVA百科hetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I'm working on something like recycle bin, that I need to move folder/files to already existing folder, I tried to use

Directory.Move

but it creates new directory and that's wrong for me, I have a specific directory to move to. Can you help me?


It seems do don't actually want to move the folder, you want to move the contents of the folder. If you want to do that, you have to tell the computer to do that:

void MoveContentsOfDirectory(string source, string target)
{
    foreach (var file in Directory.EnumerateFiles(source))
    {
        var dest = Path.Combine(target, Path.GetFileName(file));
        File.Move(file, dest);
    }

    foreach (var dir in Directory.EnumerateDirectories(source))
    {
        var dest = Path.Combine(target, Path.GetFileName(dir));
        Directory.Move(dir, dest);
    }

    // optional
    Directory.Delete(source);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消