WPF: Folder Dialog und Get Files
Aufgabe: Verzeichnis aus einem Verzeichnis-Dialog übernehmen
Unter WPF gibt es leider keinen integrierten FolderDialog. Man muss deshalb den FolderBrowserDialog aus Windows Forms übernehmen.
Diesen bindet man ein wie
System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
|
Den ausgewählten Ordner kann man übernehmen mit
String sPath= folderDialog.SelectedPath;
|
Betrifft: WPF, FolderBrowserDialog, Folder; Dialog; FolderDialog; Windows Forms
C# Code Beispiel:
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 >
|
Mit verwendung des Namespaces 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 >--
|
Hinweis: hierzu muss man zusätzlich unter Project->References mit Add Reference einen Verweis zu System.Windows.Forms hinzufügen.
Der Verzeichnis-Dialog sieht wie hier dargestellt aus.
Anschliessend kann man das Verzeichnis unter WPF übernehmen und alle Dateien auflisten
Ein Verzeichnis wird in WPF gelesen mit DirectoryInfo
DirectoryInfo folder = new DirectoryInfo(sPath);
|
Hierzu benötigt man den Namespace
using System.IO; //Folder, Directory
|
Anschliessend kann man die Dateien, welche sich in diesem Verzeichnis befinden, auflisten
foreach (FileInfo fileInfo in folder.GetFiles())
{
..
}
|
C#, WPF Code-Beispiel zum folgenden Durchlaufen der beinhalteten Dateien
//--------< 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
Folgend das komplette 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 >----
}
}
}
}
|
Und das gezeigte WPF Frontend unter 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:
Mit FileInfo werden alle Informationen der Datei angezeigt