我试图从 C# 中的打开文件对话框中选择一个目录,但是当我实际将项目添加到列表视图时,它没有被显示。到目前为止,这是我的代码......我错过了什么吗?
private void btnChoose_Click(object sender, EventArgs e)
{
try
{
FolderBrowserDialog folderPicker = new FolderBrowserDialog();
listLeft.View = View.Details;
if (folderPicker.ShowDialog() == DialogResult.OK)
{
DirectoryInfo di = new DirectoryInfo(folderPicker.SelectedPath);
FileInfo[] files = di.GetFiles();
DirectoryInfo[] directories = di.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
listLeft.Items.Add("Directory " + directory.Name);
}
foreach (FileInfo file in files)
{
listLeft.Items.Add(file.Name);
}
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
编辑::: 添加了这个带有描述的截图......