-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
105 lines (93 loc) · 3.34 KB
/
MainWindow.xaml.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace 文件浏览
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
FolderBrowserDialog m_Dialog = new FolderBrowserDialog(); //new一个文件夹
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult result = m_Dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel) return;
string m_Dir = m_Dialog.SelectedPath.Trim();
textbox1.Text = m_Dir;
// TextBox_TextChanged = m_Dir;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
textbox2.Text = "";
string m_Dir = m_Dialog.SelectedPath.Trim();
if (m_Dir.Length != 0) //判断用户是否选择了一个文件夹
{
string[] files = System.IO.Directory.GetFiles(m_Dir, "*.*"); //获取m_Dir目录下的所有文件
foreach (string s in files) //循环输出结果
{
System.IO.FileInfo fi = null;
try
{
fi = new System.IO.FileInfo(s); //将文件依次赋给fi
}
catch (System.IO.FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
continue;
}
textbox2.Text += m_Dir + "\\" + fi.Name;
textbox2.Text += Environment.NewLine;
}
}
else
{
textbox2.Text += "请先选择一个文件夹";
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
textbox2.Text = "";
string m_Dir = m_Dialog.SelectedPath.Trim();
if (m_Dir.Length != 0) //判断用户是否选择了一个文件夹
{
string[] files = System.IO.Directory.GetDirectories(m_Dir, "*.*"); //获取m_Dir目录下的所有子文件夹
foreach (string s in files) //循环输出结果
{
System.IO.FileInfo fi = null;
try
{
fi = new System.IO.FileInfo(s); //将文件依次赋给fi
}
catch (System.IO.FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
continue;
}
textbox2.Text += m_Dir + "\\" + fi.Name;
textbox2.Text += Environment.NewLine;
}
}
else
{
textbox2.Text += "请先选择一个文件夹";
}
}
}
}