如何将文件夹移动到另一个位置 [英] How to move folder to another location

查看:130
本文介绍了如何将文件夹移动到另一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的应用程序中,我必须将文件夹移动到另一个位置,但确实发生了问题,但是当我要将文件夹从C:/...移至D:/...或反之亦然时.

它是从C:/到C:/的作品.

任何解决此问题的方法,我将不胜感激.

TIA.

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.

推荐答案

这里是另一种方法.使用此导出的函数创建一个C DLL:

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);

}



然后P/从C#调用它:



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");
}



请注意,我在字符串中添加了额外的NULL.这是必需的,不是错字.



Notice the extra NULLs I added to the string. That is required and is not a typo.


您使用的是Directory.Move吗?如果是这样,尝试移动到其他驱动器时会遇到什么错误?

更多信息:

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

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

[更新]

是的,这不适用于所有卷.您可以递归创建目录并复制文件,然后删除源目录.这项工作有些繁琐,但是Google进行了快速搜索,发现了一些现有的解决方案(人们已经编写了递归方法来做到这一点.).
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).


这篇关于如何将文件夹移动到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆