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.