在客户端更新时,用到文件夹复制,因此复习下C#中对文件的操作(using System.IO;)
1
1/**//// <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) != "\\")
10
10 {
11
11 sourceDirName = sourceDirName + "\\";
12
12 }
13
13 if (destDirName.Substring(destDirName.Length - 1) != "\\")
14
14 {
15
15 destDirName = destDirName + "\\";
16
16 }
17
17
18
18 if (Directory.Exists(sourceDirName))
19
19 { //如果不存在 创建文件夹
20
20 if(!Directory.Exists(destDirName))
21
21 {
22
22 Directory.CreateDirectory(destDirName);
23
23 }
24
24 foreach (string item in Directory.GetFiles(sourceDirName))
25
25 { //文件复制
26
26 File.Copy(item,destDirName+Path.GetFileName(item),true);
27
27 }
28
28 foreach (string item in Directory.GetDirectories(sourceDirName))
29
29 { //递归文件夹
30
30 Copy(item, destDirName + item.Substring(item.LastIndexOf("\\")+ 1));
31
31 }
32
32 }
33
33 }
1/**//// <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) != "\\")10
10 {11
11 sourceDirName = sourceDirName + "\\";12
12 }13
13 if (destDirName.Substring(destDirName.Length - 1) != "\\")14
14 {15
15 destDirName = destDirName + "\\";16
16 }17
1718
18 if (Directory.Exists(sourceDirName))19
19 { //如果不存在 创建文件夹20
20 if(!Directory.Exists(destDirName))21
21 {22
22 Directory.CreateDirectory(destDirName);23
23 }24
24 foreach (string item in Directory.GetFiles(sourceDirName))25
25 { //文件复制26
26 File.Copy(item,destDirName+Path.GetFileName(item),true);27
27 }28
28 foreach (string item in Directory.GetDirectories(sourceDirName))29
29 { //递归文件夹30
30 Copy(item, destDirName + item.Substring(item.LastIndexOf("\\")+ 1));31
31 }32
32 }33
33 }选择一个文件,得到这个文件所在文件夹下的所有文件
1
if (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) 能得到文件的全名,不要前面 的路径


