zoukankan      html  css  js  c++  java
  • C#对文件进行操作

    在客户端更新时,用到文件夹复制,因此复习下C#中对文件的操作(using System.IO;

     

     11/**//// <summary>
     2 2    /// 文件夹复制
     3 3    /// </summary>
     4 4    /// <param name="sourceDirName">原始路径</param>
     5 5    /// <param name="destDirName">目标路径</param>
     6 6    /// <returns></returns>
     7 7    public static void Copy(string sourceDirName, string destDirName)
     8 8    {
     9 9        if (sourceDirName.Substring(sourceDirName.Length - 1!= "\\")
    1010        {
    1111            sourceDirName = sourceDirName + "\\";
    1212        }

    1313        if (destDirName.Substring(destDirName.Length - 1!= "\\")
    1414        {
    1515            destDirName = destDirName + "\\";
    1616        }

    1717
    1818        if (Directory.Exists(sourceDirName))
    1919        //如果不存在 创建文件夹
    2020            if(!Directory.Exists(destDirName))
    2121            {
    2222                Directory.CreateDirectory(destDirName);
    2323            }

    2424            foreach (string item in Directory.GetFiles(sourceDirName))
    2525            {  //文件复制
    2626                File.Copy(item,destDirName+Path.GetFileName(item),true);
    2727            }

    2828            foreach (string item in Directory.GetDirectories(sourceDirName))
    2929            {  //递归文件夹
    3030                Copy(item, destDirName + item.Substring(item.LastIndexOf("\\")+ 1));
    3131            }

    3232        }

    3333    }

    选择一个文件,得到这个文件所在文件夹下的所有文件


     1if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
     2            {
     3                foreach (string s in openFileDialog1.FileNames)
     4                {
     5                    // E:\\working\\DLL及配置文件\\
     6                    strPath = s.Substring(0, s.LastIndexOf("\\")); //"E:\\working\\DLL及配置文件\\Help"
     7                    if (strPath.Substring(strPath.Length - 1!= "\\")
     8                    {
     9                        strPath = strPath + "\\";
    10                    }

    11                    foreach (string item in Directory.GetFiles(strPath))
    12                    {
    13                        this.listBox1.Items.Add(item);
    14                    }

    15
    16                    foreach (string path in Directory.GetDirectories(strPath))
    17                    {
    18                        foreach (string item1 in Directory.GetFiles(path))
    19                        {
    20                            this.listBox1.Items.Add(item1);
    21                        }

    22                    }

    23
    24                }

    25            }

     

     Path.GetFileName(strList)  能得到文件的全名,不要前面 的路径

  • 相关阅读:
    用 PHP 自带函数 fputcsv 和 fgetcsv 来导出和导入csv
    Node express 框架
    ES6语法及JS语言的其他特性
    Node模块化及CommonJS规范
    Nodemon 开发环境自动重启服务工具
    Node 使用模板引擎art-template
    npm的使用
    leetcode刷题笔记 二百零四题 计数质数
    leetcode刷题笔记 二百零三题 移除链表元素
    leetcode刷题笔记 二百零二题 快乐数
  • 原文地址:https://www.cnblogs.com/lhjhl/p/1306106.html
Copyright © 2011-2022 走看看