winform选择文件夹读取文件名
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45private void button1_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { string foldPath = folderBrowserDialog1.SelectedPath; MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //获取该目录下的文件 string[] files = Directory.GetFiles(@foldPath + "\", "*.bmp", System.IO.SearchOption.TopDirectoryOnly); List<string> fileNameList = new List<string>(); foreach (var f in files) { Console.WriteLine(f); string fileName = System.IO.Path.GetFileName(f); //获取文件名 fileName = fileName.Substring(0, fileName.IndexOf('.')); // 去除尾缀 fileNameList.Add(fileName); }; List<string> rowNameList = new List<string>(); List<string> colNameList = new List<string>(); foreach (string fileName in fileNameList) { Console.WriteLine(fileName); //遍历打印出动态数组list中的元素 string rowName = fileName.Substring(0, 2); //文件路径 string colName = fileName.Substring(2); //文件路径 rowNameList.Add(rowName); colNameList.Add(colName); } foreach (string fileName in rowNameList) { Console.WriteLine(fileName); //遍历打印出动态数组list中的元素 } foreach (string fileName in colNameList) { Console.WriteLine(fileName); //遍历打印出动态数组list中的元素 } // 读取excel文件 } }
选择文件路径
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19private void button1_Click(object sender, EventArgs e) { try { OpenFileDialog OpenImage = new OpenFileDialog(); // OpenImage.Filter = "(位图)*.bmp|"; if (OpenImage.ShowDialog() == DialogResult.OK) { string res = OpenImage.FileName; MessageBox.Show(res); } } catch { MessageBox.Show("失败"); } }
最后
以上就是搞怪小鸭子最近收集整理的关于winform选择文件夹读取文件名和文件路径的全部内容,更多相关winform选择文件夹读取文件名和文件路径内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复