C#以排列组合中的“组合”方式遍历某个文件夹下的所有文件
2015-07-08 10:22
696 查看
参考自:http://blog.csdn.net/zmazon/article/details/8315418#comments
程序部分输出结果如下(C(3,4),从文件夹中的4个文件中选出r=3的组合):
private void button1_Click(object sender, EventArgs e) { //temp文件夹下存放着我们的环境因子 DirectoryInfo folder = new DirectoryInfo(@"E:\work\test\data\temp"); string fileName; FileInfo[] file = folder.GetFiles("*.asc");//只读取asc文件 string[] AttributeFiles = new string[file.Length]; for (int i = 0; i < file.Length; i++) { fileName = folder + "\\" + file[i].ToString(); AttributeFiles[i] = fileName; } int r = 3;//组合数,即C(r,n)中的r if (null == AttributeFiles || AttributeFiles.Length == 0 || r <= 0 || r > AttributeFiles.Length) { MessageBox.Show("请检查文件夹的文件是否为空,参数n的设置是否有误。"); } string[] AttributeFilesResult = new string[r]; getCombination(AttributeFiles, r, 0, AttributeFilesResult, 0); } private void getCombination(string[] a, int r, int begin, string[] b, int index) { string AttributeFile = ""; if (r == 0) { for (int i = 0; i < index; i++) { AttributeFile = AttributeFile + b[i] + " "; } Console.WriteLine("AttributeFile=" + AttributeFile); } for (int i = begin; i < a.Length; i++) { for (int j = 0; j < r; j++) { if (j % r != 0) { b[index] = a[i] + "#";//遍历文件,生成路径+文件名,在每个路径文件名后面加“#”号,末尾元素不加 } else { b[index] = a[i]; } } getCombination(a, r-1,i+1,b,index+1); } }
程序部分输出结果如下(C(3,4),从文件夹中的4个文件中选出r=3的组合):
AttributeFile=E:\work\test\data\temp\horizc.asc# E:\work\test\data\temp\profc.asc# E:\work\test\data\temp\slope.asc AttributeFile=E:\work\test\data\temp\horizc.asc# E:\work\test\data\temp\profc.asc# E:\work\test\data\temp\twi.asc AttributeFile=E:\work\test\data\temp\horizc.asc# E:\work\test\data\temp\slope.asc# E:\work\test\data\temp\twi.asc AttributeFile=E:\work\test\data\temp\profc.asc# E:\work\test\data\temp\slope.asc# E:\work\test\data\temp\twi.asc
相关文章推荐
- C# windows服务:通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
- C#读写txt文件的两种方法介绍
- c#读取dbf文件并输出某一列
- c#猜猜看
- c# 图片转二进制/字符串 二进制/字符串反转成图片
- 11-C#反射机制
- C#调用WebService实例和开发(初学者使用)
- C#课程设计——“猜猜看”开发总结
- c#第五次作业
- C#第二次作业:WinForm可视化设计:记事本开发和顺序图片阅读器
- Csharp学习第二天
- C#第五次作业:开源项目“网页正文提取”的理解
- Csharp学习第一天
- c#第四次作业
- C#第三次作业
- C#程序设计课程总结
- Singleton设计模式的C#实现
- C#中正则表达式的使用
- C#语句
- C#语言基础