WPF,如何调用 OpenFolderDialog 并在列表框中加载一个或多个文件夹的内容......

Hui Liu-MSFT 40,786 信誉分 Microsoft 供应商
2024-04-05T07:42:23.51+00:00

亲爱的朋友们,早上好。 我有一个小项目。它是 ListBox 中文本文件名的加载器,这反过来又允许我选择它们,然后能够将它们记录或写入 * 中。已选择其路径的 INI 文件。

“打开文件夹”按钮显示一个 OpenFolderDialog 并在 TextBox 中加载所选文件夹的路径,这反过来又在 LisBox 中加载位于所选文件夹以及主文件夹的子文件夹中的所有文本files.txt。

例如,在 ListBox 中选择的文本将写入或保存在 TextList.ini 文件中,而未选择的文本将从TextList.ini文件中删除。请您能帮助我,我还制作了一个视频,以便我可以有更好的参考,我还附上了一些图像。

视频链接:https://www.youtube.com/watch?v=bfoQ0GijDgk

Note:此问题总结整理于:WPF, How can I call an OpenFolderDialog and load the content of a folder or subfolders in a listbox ...

Windows Presentation Foundation
Windows Presentation Foundation
.NET Framework 的一部分,它提供统一的编程模型,用于在 Windows 上构建业务线桌面应用程序。
58 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 34,276 信誉分 Microsoft 供应商
    2024-04-05T08:00:07.2533333+00:00

    在项目引用中添加System.Windows.Forms.dll后,可以同时使用 OpenFolderDialog 和 FolderBrowserDialog。 用于获取文件夹位置的代码:

     private void btnOpen_Click(object sender, RoutedEventArgs e)  
            {  
                System.Windows.Forms.FolderBrowserDialog openFileDlg = new System.Windows.Forms.FolderBrowserDialog();  
                var result = openFileDlg.ShowDialog();  
                if (result.ToString() != string.Empty)  
                {  
                    txtPath.Text = openFileDlg.SelectedPath;  
                }  
                root = txtPath.Text;  
            }  
    

    用于获取文件夹和子文件夹中的 txt 的代码:

    private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                lt = new ObservableCollection<MyModel>();  
                string[] dicFileList = Directory.GetFiles(root, "*.txt", SearchOption.AllDirectories);  
                foreach (string element in dicFileList)  
                {  
                    myModel = new MyModel();  
                    //myModel.Name = System.IO.Path.GetFileName(element);  
                    myModel.Name = System.IO.Path.GetFileNameWithoutExtension(element);  
                    myModel.StatusForCheckBox = false;  
      
                    lt.Add(myModel);  
                }  
                myList.ItemsSource = lt;  
            }  
    

    您可以创建一个字段 public bool StatusForCheckBox { get; set;} 以便 MyModel 标识是否被选中:

     private void Save_Click(object sender, RoutedEventArgs e)  
            {  
                List<string> ltForSave = new List<string>();  
                foreach (MyModel obj in myList.ItemsSource)  
                {        
                    if(obj.StatusForCheckBox==false)  
                    {  
                        ltForSave.Add(";" + obj.Name);  
                    }else  
                    {  
                        ltForSave.Add(obj.Name);  
                    }  
                }  
                //Save ltForSave as ini file.      
            }  
    

    使用列表框,如下所示:

    <ListBox x:Name="myList" Width="400" Height="400" HorizontalAlignment="Right">  
                <ListBox.ItemTemplate>  
                    <DataTemplate >  
                        <CheckBox Content="{Binding Name}" IsChecked="{Binding StatusForCheckBox,Mode=TwoWay}"/>  
                    </DataTemplate>  
                </ListBox.ItemTemplate>  
            </ListBox>  
    

    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助