WPF, How can I call an OpenFolderDialog and load the content of a folder or subfolders in a listbox ...

jack pelon 96 Reputation points
2020-09-06T18:07:35.927+00:00

Good morning dear friends.
I have a small project. It is a loader of text file names in a ListBox and that in turn allows me to select them and then be able to record or write them in a * .INI file that is already selected with its path.

The "Open Folder" button shows an OpenFolderDialog and loads the path of the selected folder in a TextBox and this in turn loads in the LisBox all the text files.txt located in the chosen folder and also in the subfolders of the main folder .

The texts selected in the ListBox are written or saved in a TextList.ini file (for example), and those that are not selected are deleted from the TextList.ini file. Please I hope you can help me, I have also created a video so that I can have a better reference and I have attached some images as well.

Video Link: https://www.youtube.com/watch?v=bfoQ0GijDgk

e42f586aa66cd61029bbc5fbdde319ea.jpg

Developer technologies | Windows Presentation Foundation
{count} votes

Answer accepted by question author
  1. DaisyTian-1203 11,651 Reputation points Moderator
    2020-09-10T02:43:41.573+00:00

    You can use both OpenFolderDialog and FolderBrowserDialog after you adding the System.Windows.Forms.dll in the project's reference.
    Code to get the folder location:

     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;  
            }  
    

    Code to get the txt in folder and subfolders:

    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;  
            }  
    

    You can create a field public bool StatusForCheckBox { get; set;} for MyModel to identity selected or not:

     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.      
            }  
    

    Use the listbox like:

    <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>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. David Reinke 20 Reputation points
    2023-11-28T23:23:36.8366667+00:00

    This doesn't work for me in Visual Studio 2022. I found a reference to System.Windows.Forms.VisualStudio.15.0.dll, so I added that. Trouble is, it didn't work. When I try to put in a using statement, it doesn't recognize System.Windows.Forms.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.