Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

WPF: FolderDialog and GetFiles
 
Task: Import directory from a directory dialog
 
Under WPF there is unfortunately no integrated FolderDialog. You must therefore take the FolderBrowserDialog from Windows Forms.
This one binds like

System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();

 
The selected folder can be adopted with

String sPath= folderDialog.SelectedPath;

 
 
 
Subject: WPF, FolderBrowserDialog, Folder; Dialog; FolderDialog; Windows Forms
 
C # Code example:
WPF Folderdialog:

WinForms.FolderBrowserDialog folderDialog = new WinForms.FolderBrowserDialog();
folderDialog.ShowNewFolderButton = false;
folderDialog.SelectedPath = System.AppDomain.CurrentDomain.BaseDirectory;
WinForms.DialogResult result = folderDialog.ShowDialog();

if (result == WinForms.DialogResult.OK )
{
//----< Selected Folder >----
//< Selected Path >
String sPath= folderDialog.SelectedPath;
tbxFolder.Text = sPath;
//</ Selected Path >

 
Using the Namespace System.Windows.Forms

//--< using >--
using Microsoft.Win32; //FileDialog
using WinForms = System.Windows.Forms ; //FolderDialog
using System.IO; //Folder, Directory
using System.Diagnostics; //Debug.WriteLine
//--</ using >--

Note: To do this, add a reference to System.Windows.Forms under Project-> References with Add Reference.
 
 
The directory dialog looks as shown here.

 
 
Afterwards, you can transfer the directory under WPF and list all files
 
A directory is read in WPF using DirectoryInfo

DirectoryInfo folder = new DirectoryInfo(sPath);
 

You need the namespace

using System.IO; //Folder, Directory
 

 
Then you can list the files, which are in this directory

foreach (FileInfo fileInfo in folder.GetFiles())
{
..
}

 
 
C #, WPF Code example for traversing the included files

//--------< Folder >--------
DirectoryInfo folder = new DirectoryInfo(sPath);
if (folder.Exists)
{
//------< @Loop: Files >------
foreach (FileInfo fileInfo in folder.GetFiles())
{
//----< File >----
String sDate = fileInfo.CreationTime.ToString("yyyy-MM-dd") ;
Debug.WriteLine("#Debug: File: " + fileInfo.Name + " Date:" + sDate);
//----</ File >----
}
//------</ @Loop: Files >------
}
//--------</ Folder >--------
 

 
 
 
Video Tutorial

 
Following the complete code snippet in C #, WPF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
 
//--< using >--
using Microsoft.Win32; //FileDialog
using WinForms = System.Windows.Forms ; //FolderDialog
using System.IO; //Folder, Directory
using System.Diagnostics; //Debug.WriteLine
//--</ using >--
 
namespace Gruppiere_Verzeichnis
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
 
private void btnSelect_Folder_Click(object sender, RoutedEventArgs e)
{
WinForms.FolderBrowserDialog folderDialog = new WinForms.FolderBrowserDialog();
folderDialog.ShowNewFolderButton = false;
folderDialog.SelectedPath = System.AppDomain.CurrentDomain.BaseDirectory;
WinForms.DialogResult result = folderDialog.ShowDialog();

if (result == WinForms.DialogResult.OK )
{
//----< Selected Folder >----
//< Selected Path >
String sPath= folderDialog.SelectedPath;
tbxFolder.Text = sPath;
//</ Selected Path >
 
//--------< Folder >--------
DirectoryInfo folder = new DirectoryInfo(sPath);
if (folder.Exists)
{
//------< @Loop: Files >------
foreach (FileInfo fileInfo in folder.GetFiles())
{
//----< File >----
String sDate = fileInfo.CreationTime.ToString("yyyy-MM-dd") ;
Debug.WriteLine("#Debug: File: " + fileInfo.Name + " Date:" + sDate);
//----</ File >----
}
//------</ @Loop: Files >------
}
//--------</ Folder >--------
//----</ Selected Folder >----
}
}
}
}
 
 
 

 
 
And the WPF frontend shown under XAML

<Window x:Class="Gruppiere_Verzeichnis.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Gruppiere_Verzeichnis"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Name="gridBase">
<Grid.RowDefinitions>
<RowDefinition Name="rowSelect" Height="11*"/>
<RowDefinition Height="96*"/>
</Grid.RowDefinitions>
<Grid Name="gridSelect" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="192"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Grid.Row="1">
<Button x:Name="btnSelect_Folder" Content="Ordner" Click="btnSelect_Folder_Click" Width="64" />
<TextBox x:Name="tbxFolder" TextWrapping="Wrap" Text="" Width="432" />
 
</StackPanel>
</Grid>
<Grid Name="gridFolders" Grid.Row="0">
 
</Grid>
</Grid>
</Window>
 

 
 
 
Debug:
FileInfo displays all information about the file
Mobile
»
WPF: Explorer with path open
»
WPF: Open a Window at a position
»
WPF: Selection enter open and well take over with Button Elements
»
WPF: Select Folder with Startup Directory
»
WPF: FolderDialog and GetFiles
»
Error CS0234: The type or namespace name Forms does not exist in the namespace
»
WPF: Filedialog, OpenFileDialog

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de