C#对文件进行操作

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

 

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

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

1717
1818        if (Directory.Exists(sourceDirName))
19ExpandedSubBlockStart.gifContractedSubBlock.gif19        //如果不存在 创建文件夹
2020            if(!Directory.Exists(destDirName))
21ExpandedSubBlockStart.gifContractedSubBlock.gif21            {
2222                Directory.CreateDirectory(destDirName);
2323            }

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

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

3232        }

3333    }

 

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

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

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

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

22                    }

23
24                }

25            }

 

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

转载于:https://www.cnblogs.com/lhjhl/archive/2008/10/08/1306106.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值