把这个 string foldPath变量写到方法的外面就可以了
[C#] 纯文本查看 复制代码
string foldPath="";
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo theFolder = new DirectoryInfo(foldPath);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
// this.listBox1.Items.Add(NextFolder.Name);
FileInfo[] fileInfo = NextFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍历文件
this.listBox1.Items.Add(NextFile.Name);
}
}
private void button2_Click(object sender, EventArgs e)
{
/*
*
*选择文件夹
*
*/
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == DialogResult.OK)
{
foldPath = dialog.SelectedPath;
MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
|