栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

C# 目录复制、目录移动

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 目录复制、目录移动

/// 
/// 判断目标是文件夹还是目录(目录包括磁盘)
/// 
/// 路径
/// 返回true为一个文件夹,返回false为一个文件
public static bool IsDir(string filepath)
{
    FileInfo fi = new FileInfo(filepath);
    if ((fi.Attributes & FileAttributes.Directory) != 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/// 
/// 目录复制
/// 
/// 目录路径
/// 目标目录路径
/// 复制模式(0:默认,整个目录复制到目标目录下;1:目录下所有文件与目录复制到目标目录下;)
/// 是否删除源文件
private void DirectoryCopy(string dirPath, string targetDirPath, int copyModel = 0, bool deleteOriginFile = false)
{
    if (copyModel == 1)
    {
        // 创建目标目录
        Directory.CreateDirectory(targetDirPath);

        // 获取目录下所有文件与目录
        string[] fileArr = Directory.GetFiles(dirPath);
        string[] dirArr = Directory.GetDirectories(dirPath);

        foreach (string file in fileArr)
        {
            FileInfo fi = new FileInfo(file);
            File.Copy(file, Path.Combine(targetDirPath, fi.Name));
            if (deleteOriginFile)
            {
                File.Delete(file);
            }
        }
        foreach (string dir in dirArr)
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            DirectoryCopy(Path.Combine(dirPath, di.Name), Path.Combine(targetDirPath, di.Name), copyModel, deleteOriginFile);
            if (deleteOriginFile)
            {
                Directory.Delete(dir);
            }
        }
    }
    else
    {
        DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
        targetDirPath = Path.Combine(targetDirPath, dirInfo.Name);

        // 创建输出
        Directory.CreateDirectory(targetDirPath);

        // 获取目录下所有文件与目录
        string[] fileArr = Directory.GetFiles(dirPath);
        string[] dirArr = Directory.GetDirectories(dirPath);

        foreach (string file in fileArr)
        {
            FileInfo fi = new FileInfo(file);
            string outFilePath = Path.Combine(targetDirPath, fi.Name);
            if (File.Exists(outFilePath))
            {
                File.Delete(outFilePath);
            }
            File.Copy(file, outFilePath);
            if (deleteOriginFile)
            {
                File.Delete(file);
            }
        }
        foreach (string dir in dirArr)
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            DirectoryCopy(Path.Combine(dirPath, di.Name), targetDirPath, copyModel, deleteOriginFile);
            if (deleteOriginFile)
            {
                Directory.Delete(dir);
            }
        }
    }
}


/// 
/// 移动
/// 
/// 源路径
/// 目标路径
/// 目录移动模式;0:默认-源目录移动至目标目录下;1:源目录下所有文件及目录移动至目标目录下;
/// 已知错误
private void FileMove(string originPath, string targetPath, int dirMoveMode = 0)
{
    // 目录判断
    bool originIsDir = IsDir(originPath);
    bool targetIsDir = IsDir(targetPath);
    // 合理性检查
    if (originIsDir && targetIsDir) // 源路径为目录 目标路径为目录
    {
        switch (dirMoveMode)
        {
            case 1:
                // 获取源路径下所有文件
                string[] files = Directory.GetFiles(originPath);
                // 获取源路径下所有目录
                string[] dirs = Directory.GetDirectories(originPath);
                try
                {
                    // 源路径下所有文件 移动至 目标路径下
                    foreach (string file in files)
                    {
                        FileInfo fi = new FileInfo(file);
                        if (File.Exists(targetPath + @"" + fi.Name))
                        {
                            File.Delete(targetPath + @"" + fi.Name);
                        }
                        File.Move(file, targetPath + @"" + fi.Name);
                    }
                    // 源路径下所有目录 移动至 目标路径下
                    foreach (string dir in dirs)
                    {
                        DirectoryInfo di = new DirectoryInfo(dir);
                        if (Directory.Exists(targetPath + @"" + di.Name))
                        {
                            Directory.Delete(targetPath + @"" + di.Name, true);
                        }
                        Directory.Move(dir, targetPath + @"" + di.Name);
                    }
                }
                catch (IOException ex)
                {
                    if (ex.Message.Contains("源路径和目标路径必须具有相同的根。"))
                    {
                        // 复制目录
                        DirectoryCopy(originPath, targetPath, 1, true);
                    }
                    else
                    {
                        throw ex;
                    }
                }
                break;
            default:
                // 源目录移动至目标目录下
                try
                {
                    DirectoryInfo df = new DirectoryInfo(originPath);
                    Directory.Move(originPath, targetPath + @"" + df.Name);
                }
                catch (IOException ex)
                {
                    if (ex.Message.Contains("源路径和目标路径必须具有相同的根。"))
                    {
                        // 复制目录
                        DirectoryCopy(originPath, targetPath, 0, true);
                    }
                    else
                    {
                        throw ex;
                    }
                }
                break;
        }
    }
    else if (!originIsDir && targetIsDir) // 源路径非目录 目标路径为目录
    {
        // 获取文件名
        FileInfo fi = new FileInfo(originPath);
        // 检查目标路径下是否存在同名文件
        if (File.Exists(targetPath + @"" + fi.Name))
        {
            // 存在则删除
            File.Delete(targetPath + @"" + fi.Name);
        }

        try
        {
            // 源文件 移动至 目标路径下
            File.Move(originPath, targetPath + @"" + fi.Name);
        }
        catch (IOException ex)
        {
            if (ex.Message.Contains("源路径和目标路径必须具有相同的根。"))
            {
                File.Copy(originPath, targetPath + @"" + fi.Name);
                File.Delete(originPath);
            }
            else
            {
                throw ex;
            }
        }
    }
    else if (!originIsDir && !targetIsDir) // 源路径非目录 目标路径非目录
    {
        // 检查目标文件是否存在
        if (File.Exists(targetPath))
        {
            // 存在则删除
            File.Delete(targetPath);
        }

        try
        {
            // 源文件 移动至 目标文件
            File.Move(originPath, targetPath);
        }
        catch (IOException ex)
        {
            if (ex.Message.Contains("源路径和目标路径必须具有相同的根。"))
            {
                File.Copy(originPath, targetPath);
                File.Delete(originPath);
            }
            else
            {
                throw ex;
            }
        }
    }
    else
    {
        throw new Exception("源路径为目录时,目标路径不可为文件!");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/959114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号