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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,795 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    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. jack pelon 96 Reputation points
    2020-09-08T01:37:11.35+00:00

    Hi well, the problem is that WPF does not have an "OpenFolderDialog", or "Browser Folder Dialog", is there any way that a button can be implemented in my project that shows me the path of a folder in a "textbox", and looks for * .txt files inside of the folder and subfolders, which I would like to see in a Listbox with checkboxes.

    • These same names selected in the "checkbox", of the "listbox", I want to write them in a * .ini file. For example: "list of names.ini"

    The file "list of names.ini" must have the following parameters:

    [Super_Hero.List]
    ActiveText = Batman
    ActiveText = Superman
    ActiveText = Spaiderman

    I also have a button which updates the listbox to add or remove selected names and another button that saves the file "list of names.ini"

    I hope it can be done and you have the generous help you can give me, thank you very much in advance friend, PeterFleischer-3316

    I made a video with powerpoint to illustrate the project.

       Video Link:        https://www.youtube.com/watch?v=bfoQ0GijDgk
    
    1 person found this answer helpful.
    0 comments No comments

  2. DaisyTian-1203 11,626 Reputation points
    2020-09-08T08:04:03.057+00:00

    You can use System.Windows.Forms.FolderBrowserDialog to open the folder, and you can use it like below to get the path.

    System.Windows.Forms.FolderBrowserDialog openFileDlg = new System.Windows.Forms.FolderBrowserDialog();  
                var result = openFileDlg.ShowDialog();  
                if (result.ToString() != string.Empty)  
                {  
                    txtPath.Text = openFileDlg.SelectedPath;  
                }  
    

    I will show you my demo to get content for folders and subfolders.
    The code for MainWindow.xaml

     <Grid>  
            <TextBox Width="200" Height="35" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0 20 0 0" Name="txtPath"></TextBox>  
            <Button Content="Open" Width="80" Height="35" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="250 20 0 0" Name="btnOpen" Click="btnOpen_Click" />  
            <Button Width="100" Height="50" Content="ShowFolder" Click="Button_Click" HorizontalAlignment="Left"></Button>  
            <Button Width="200" Height="40" Content="ShowFolder+SubFolder" Click="Button_Click2" HorizontalAlignment="Left" Margin="1,245,0,124"></Button>  
            <ListBox x:Name="myList" Width="400" Height="400" HorizontalAlignment="Right">  
                <ListBox.ItemTemplate>  
                    <DataTemplate >  
                        <CheckBox  Content="{Binding Name}"/>  
                    </DataTemplate>  
                </ListBox.ItemTemplate>  
            </ListBox>  
        </Grid>  
    

    The code for MainWindow.xaml.cs:

    public partial class MainWindow : Window  
        {  
            string root = "";  
             MyModel myModel;  
            public MainWindow()  
            {  
                InitializeComponent();  
                 
            }  
            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;  
            }  
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                //Show Folders  
                List<MyModel> lt = new List<MyModel>();  
                System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(root);  
                System.IO.DirectoryInfo[] ds = d.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly);  
                foreach (System.IO.DirectoryInfo var in ds)  
                {  
                    myModel = new MyModel();  
                    myModel.Name = var.Name;  
                    lt.Add(myModel);  
                }  
                myList.ItemsSource = lt;   
            }  
            private void Button_Click2(object sender, RoutedEventArgs e)  
            {  
                //Show all Folders and SubFolder  
                List<MyModel> myModels = new List<MyModel>();  
                GetAllFolder(root, myModels);  
                myList.ItemsSource = myModels;  
            }  
            public List<MyModel> GetAllFolder(string path, List<MyModel> myModels)  
            {  
                System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(path);  
                System.IO.DirectoryInfo[] ds = d.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly);  
                FileInfo[] fil = d.GetFiles();  
                DirectoryInfo[] dii = d.GetDirectories();  
      
                foreach (System.IO.DirectoryInfo var in ds)  
                {  
                    myModel = new MyModel();  
                    myModel.Name = var.Name;  
                    myModels.Add(myModel);  
                }  
                foreach (DirectoryInfo dd in dii)  
                {  
                    GetAllFolder(dd.FullName, myModels);  
                }  
                 
                return myModels;  
            }  
      
             
        }  
        public class MyModel  
        {  
            public string Name { get; set; }  
        }  
    

    The reuslt picture is like below shown:
    23266-4.gif

    1 person found this answer helpful.

  3. jack pelon 96 Reputation points
    2020-09-11T06:52:47.453+00:00

    Miss "DaisyTian-MSFT", good morning.
    I appreciate your help, and managed to open and reproduce the first part of your demo and make the openfolder dialog and subfolders work, I had to attach the following using:

    using System.IO;
    using WinForms = System.Windows.Forms;
    using Microsoft.Win32;
    using System.Diagnostics;

    But I still can't make it so that I can filter the text files and that they appear in the "Listbox" and I can also save them in a * .ini file, could you show me that in more detail, I send you the project so that you can see it and help me please and thank you very much for your time.

    I put the link of the project. I use visual studio 2019 wpf.

    view

    https://drive.google.com/file/d/1fI0QhMkJ1j8N6f1ClhSlzosnkfq47ZUM/view?usp=sharing

    1 person found this answer helpful.

  4. jack pelon 96 Reputation points
    2020-09-18T03:55:06.183+00:00

    Miss "DaisyTian-MSFT", good morning and thank you very much for your time.

    Test your Demo Project, and added an "OpenFileDialog" button to find the .ini file, for example "ListTextFile.ini" and a Textbox to specify the path of the .ini file. Which works fine, thanks to your help.
    How do I so that the names of the selected files in the Listbox can be written and saved in the * .ini file (ListTextFile.ini).

    I put two images of the project and the other of the * .ini file format inside. To be able to express myself better. I also attached a new link to the modified project so that you can do the tests.

    Link Demo Edit: https://drive.google.com/file/d/17CpKVVXZx5l7aGPd6255lkDDJUI5bRRV/view?usp=sharing

    25724-3.jpg

    1 person found this answer helpful.

Your answer

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