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)  能得到文件的全名,不要前面 的路径

  • 相关阅读:
    zzulioj1908: 小火山的围棋梦想
    zzulioj1913: 小火山的计算能力
    zullioj1905: 小火山的跳子游戏
    HDU 1025:Constructing Roads In JGShining's Kingdom
    HDU 1257:最少拦截系统
    HDU1051:Wooden Sticks
    HDU1950:Bridging signals
    HDU1087:Super Jumping! Jumping! Jumping!
    HDU5256: 序列变换
    3.SpringBoot配置文件以及自动配置原理
  • 原文地址:https://www.cnblogs.com/lhjhl/p/1306106.html
Copyright © 2011-2022 走看看