关于Windows下跨盘符移动文件夹的解决办法

关于Windows下跨盘符移动文件夹的解决办法

Windows下跨盘符直接调用Directory.Move()或DirectoryInfo的MoveTo() Method 都会抛出IO异常:源路径和目标路径必须具有相同的根。移动操作在卷之间无效。

解决办法

  1. 普遍用法:先Copy再delete
  2. 创建出文件夹结构,调用File.Move,删除原来的文件夹结构

以下是整理加工后的方法源码:

public class IOFileOper
{
    /// <summary>
    /// 递归Copy
    /// </summary>
    /// <param name="sourceFolder"></param>
    /// <param name="targetFolder"></param>
    /// <returns></returns>
    public static bool CopyFolder(string sourceFolder, string targetFolder)
    {
        try
        {
            if (targetFolder[targetFolder.Length - 1] != Path.DirectorySeparatorChar)
            {
                targetFolder += Path.DirectorySeparatorChar;
            }
            if (!Directory.Exists(targetFolder))
            {
                Directory.CreateDirectory(targetFolder);
            }
            // 得到源目录的文件列表,里面是包含文件以及目录路径的一个数组
            string[] fileList = Directory.GetFileSystemEntries(sourceFolder);
            foreach (var file in fileList)
            {
                // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                if (Directory.Exists(file))
                {
                    CopyFolder(file, targetFolder + Path.GetFileName(file));
                }
                else// 否则直接Copy文件
                {
                    File.Copy(file, targetFolder + Path.GetFileName(file), true);
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
            throw new Exception(ex.Message, ex);
        }
    }

    /// <summary>
    /// 递归Delete
    /// </summary>
    /// <param name="floderPath"></param>
    /// <returns></returns>
    public static bool DeleteFolder(string floderPath)
    {
        try
        {
            if (Directory.Exists(floderPath))
            {
                var r = Directory.GetFileSystemEntries(floderPath);
                foreach (var inst in Directory.GetFileSystemEntries(floderPath))
                {
                    if (File.Exists(inst))
                    {
                        File.Delete(inst); //直接删除其中的文件 
                    }
                    else
                    {
                        DeleteFolder(inst); //递归删除子文件夹 
                    }
                }
                //删除已空文件夹 
                Directory.Delete(floderPath);
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
            throw new Exception(ex.Message, ex);
        }
    }
    /// <summary>
    /// Directory.Delete
    /// </summary>
    /// <param name="floderPath"></param>
    /// <returns></returns>
    public static bool DeleteFolder1(string floderPath)
    {
        try
        {
            Directory.Delete(floderPath, true);
            return true;
        }
        catch (Exception ex)
        {
            return false;
            throw new Exception(ex.Message, ex);
        }
    }

    public static bool MoveFolder(string sourceFolder, string targetFolder)
    {
        // 检查目标目录是否以目录分割字符结束,如果不是则添加
        if (targetFolder[targetFolder.Length - 1] != Path.DirectorySeparatorChar)
        {
            targetFolder += Path.DirectorySeparatorChar;
        }
        if (sourceFolder[sourceFolder.Length - 1] != Path.DirectorySeparatorChar)
        {
            sourceFolder += Path.DirectorySeparatorChar;
        }

        List<string> fileNames = new List<string>();

        if (Directory.Exists(sourceFolder))
        {
            //得到所有文件路径
            fileNames.AddRange(Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories));
            if (fileNames.Count > 0)
            {
                //同盘符
                if (sourceFolder[0].Equals(targetFolder[0]))
                {
                    if (Directory.Exists(targetFolder))
                    {
                        Directory.Delete(targetFolder, true);
                    }
                    Directory.Move(sourceFolder, targetFolder);
                    return true;
                }
                else
                {
                    if (!Directory.Exists(targetFolder))
                    {
                        Directory.CreateDirectory(targetFolder);
                    }
                    DirectoryInfo dir = new DirectoryInfo(sourceFolder);
                    DirectoryInfo[] folders = dir.GetDirectories("*", SearchOption.AllDirectories);

                    for (int i = 0; i < folders.Length; i++)
                    {
                        string name = folders[i].FullName.Replace(sourceFolder, targetFolder);
                        if (!Directory.Exists(name))
                        {
                            Directory.CreateDirectory(name);
                        }
                    }

                    foreach (string filename in fileNames)
                    {
                        string fn = filename.Replace(sourceFolder, targetFolder);
                        if (File.Exists(fn))
                        {
                            File.Delete(fn);
                        }
                        File.Move(filename, filename.Replace(sourceFolder, targetFolder));
                    }
                    //删除空的目录结构
                    Directory.Delete(sourceFolder,true);
                    return true;
                }
            }
        }
        return false;
    }
}
posted @ 2015-02-02 00:23  元小宇  阅读(2266)  评论(0编辑  收藏  举报