Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
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.

TIA.
Posted

Here is an alternate approach. Create a C DLL with this exported function:

C++
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#:

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.
 
Share this answer
 
Comments
Member 7886123 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
Nish Nishant 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).
Are you using Directory.Move? If so, what error do you get when you try to move to a different drive?

More info:

http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx[^]

http://msdn.microsoft.com/en-us/library/cc148994.aspx[^]

[Update]

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).
 
Share this answer
 
v3
Comments
Member 7886123 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 10-May-11 11:05am    
this is the exact message " Source and destination path must have identical roots. Move will not work across volumes".
Nish Nishant 10-May-11 11:11am    
Yeah, check my updated answer.
Member 7886123 10-May-11 11:20am    
hhhhhm, thanx

yea, i read about this solution but i wanna some thing easier.
Nish Nishant 10-May-11 11:33am    
There's an alternate way which I'll post as an alternate solution.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900