Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# VisualStudio
in my app, i have to move a folder to another location, i did it but the problem is when i want to move the folder from C:/... to D:/... or vise versa.
it is works from C:/ to C:/.
any solution to this problem I'll be appreciate.
Posted 10-May-11 5:38am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here is an alternate approach. Create a C DLL with this exported function:
extern "C" __declspec(dllexport) void MoveDirectory(
    const wchar_t* source, const wchar_t* destination)
    SHFILEOPSTRUCT fop = {};
    fop.wFunc = FO_MOVE;
    fop.pFrom = source;
    fop.pTo = destination;
    SHFileOperation (&fop);
And P/Invoke it from C#:
[DllImport("SomeLib.dll", CharSet = CharSet.Unicode,
    CallingConvention = CallingConvention.Cdecl)]
public static extern void MoveDirectory(string source, string destination);
static void Main()
    MoveDirectory("c:\\abc\0", "d:\\abc\0");
Notice the extra NULLs I added to the string. That is required and is not a typo.
Member 7886123 at 10-May-11 12:02pm
First, thanx for your help
but when i chose a folder i did not know where the folder is(in C or in D), aaaaaaaaaaaaaaah
Nishant Sivakumar at 10-May-11 12:10pm
Not sure I understand this. If you let the user pick a folder, surely you know the full path for it (including the drive).
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Are you using Directory.Move? If so, what error do you get when you try to move to a different drive?
More info:[^][^]
Yes, that won't work across volumes. You could recursively create directories and copy files over, and then delete the source directory. It's a bit of work but a quick google search reveals a few existing solutions (people have written recursive methods to do this).
Member 7886123 at 10-May-11 10:59am
Yes, i used Directory.Move
the message " the source and the destination must be in the similar root".
Member 7886123 at 10-May-11 11:05am
this is the exact message " Source and destination path must have identical roots. Move will not work across volumes".
Nishant Sivakumar at 10-May-11 11:11am
Yeah, check my updated answer.
Member 7886123 at 10-May-11 11:20am
hhhhhm, thanx
yea, i read about this solution but i wanna some thing easier.
Nishant Sivakumar at 10-May-11 11:33am
There's an alternate way which I'll post as an alternate solution.
Member 7886123 at 10-May-11 11:25am
you know what, i have to change the file's name while moving the folder !!!!
Nishant Sivakumar at 10-May-11 11:34am
Well, that makes it crazier huh? Although you could do it after the move. See my alternate way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,870
2 Peter Leow 4,984
3 Kornfeld Eliyahu Peter 3,300
4 Maciej Los 2,456

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 10 May 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100