• 文件方法


    C# 获取指定目录下所有文件信息、移动目录、拷贝目录

    /// <summary>
    /// 获取目录下的所有文件夹和文件的path
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static List<string> getPath(string path)
    {
    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] fil = dir.GetFiles();
    DirectoryInfo[] dii = dir.GetDirectories();
    foreach (FileInfo f in fil)
    {
    list.Add(f.FullName);//添加文件的路径到列表
    }
    //获取子文件夹内的文件列表,递归遍历
    foreach (DirectoryInfo d in dii)
    {
    getPath(d.FullName);
    list.Add(d.FullName);//添加文件夹的路径到列表
    }
    return list;
    }

    /// <summary>
            /// 从一个目录将其内容移动到另一目录  
            /// </summary>
            /// <param name="directorySource">源目录</param>
            /// <param name="directoryTarget">目标目录</param>
            private void MoveFolderTo(string directorySource, string directoryTarget)
            {
                //检查是否存在目的目录  
                if (!Directory.Exists(directoryTarget))
                {
                    Directory.CreateDirectory(directoryTarget);
                }
                //先来移动文件  
                DirectoryInfo directoryInfo = new DirectoryInfo(directorySource);
                FileInfo[] files = directoryInfo.GetFiles();
                //移动所有文件  
                foreach (FileInfo file in files)
                {
                    //如果自身文件在运行,不能直接覆盖,需要重命名之后再移动  
                    if (File.Exists(Path.Combine(directoryTarget, file.Name)))
                    {
                        if (File.Exists(Path.Combine(directoryTarget, file.Name + ".bak")))
                        {
                            File.Delete(Path.Combine(directoryTarget, file.Name + ".bak"));
                        }
                        File.Move(Path.Combine(directoryTarget, file.Name), Path.Combine(directoryTarget, file.Name + ".bak"));
    
                    }
                    file.MoveTo(Path.Combine(directoryTarget, file.Name));
    
                }
                //最后移动目录  
                DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();
                foreach (DirectoryInfo dir in directoryInfoArray)
                {
                    MoveFolderTo(Path.Combine(directorySource, dir.Name), Path.Combine(directoryTarget, dir.Name));
                }
            }
    /// <summary>
            /// 从一个目录将其内容复制到另一目录
            /// </summary>
            /// <param name="directorySource">源目录</param>
            /// <param name="directoryTarget">目标目录</param>
            static void CopyFolderTo(string directorySource, string directoryTarget)
            {
                //检查是否存在目的目录  
                if (!Directory.Exists(directoryTarget))
                {
                    Directory.CreateDirectory(directoryTarget);
                }
                //先来复制文件  
                DirectoryInfo directoryInfo = new DirectoryInfo(directorySource);
                FileInfo[] files = directoryInfo.GetFiles();
                //复制所有文件  
                foreach (FileInfo file in files)
                {
                    file.CopyTo(Path.Combine(directoryTarget, file.Name));
                }
                //最后复制目录  
                DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();
                foreach (DirectoryInfo dir in directoryInfoArray)
                {
                    CopyFolderTo(Path.Combine(directorySource, dir.Name), Path.Combine(directoryTarget, dir.Name));
                }
            }
  • 相关阅读:
    用VBS脚本查询纯真IP库QQWry.dat(转)
    微软的进程监视器监视进程注册表,文件系统和线程等活动process monitor
    纯真IP库详解(转)
    VBS的开发利器WMI信息绑定参考地址
    VBS去除指定的字符串中的重复项返回重复后的字符串
    WMI中相关的操作说明execquery 或者是instancesof
    VBS字符编码的说明
    VBS中的Asc/AscB/AscW和Chr/ChrB/ChrW函数之间的区别(转)
    随笔我的想法
    关于ANSI字符编码的BUG问题(转)
  • 原文地址:https://www.cnblogs.com/CallmeYhz/p/8021924.html
Copyright © 2020-2023  润新知