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

WPF: Checking and creating folders
 
Task:
How to check with WPF, C # if a directory exists and how to create new directories.
 
Solution:
One checks for directories directly with Directory.Exists (path)
And create new folders with Directory.CreateDirectory (New_path)
 
 
Code example in C #, WPF

//--< check and create Directory >--
//*if subfolder does not exist then create one
 
String sPath_SubDirectory = folder.FullName + "\\" + sDate;
if (Directory.Exists(sPath_SubDirectory)==false )
{
Directory.CreateDirectory(sPath_SubDirectory);
}
//--</ check and create Directory >--
 

 
To use Directory and DirectoryInfo you need the Namespace System.IO

//--< using >--
using System.IO; //Folder, Directory
//--</ using >--

 
 
This example automatically creates subfolders for all photos in a directory that have the same creation date of the photos.
Applies to: WPF, Directory, Directories, Folders, Files

 
Visual Studio
Breakpoint at CreateDirectory:
If the path does not exist, then the new subfolder is created with CreateDirectory

 
 
Note: The date of a photo from a camera is in the LastWriteTime property and not in CreationTime.
CreationTime is the date used to transfer from the camera to a computer or hard drive

 
 
 
Complete sample code in C #

//----< Selected Folder >----
//< Selected Path >
String sPath = "C:\\_Daten\\Desktop\\Test_Fotos"; // 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.LastWriteTime.ToString("yyyy-MM-dd");
Debug.WriteLine("#Debug: File: " + fileInfo.Name + " Date:" + sDate);
 
//--< check and create Directory >--
//*if subfolder does not exist then create one
String sPath_SubDirectory = folder.FullName + "\\" + sDate;
if (Directory.Exists(sPath_SubDirectory)==false )
{ Directory.CreateDirectory(sPath_SubDirectory); }
//--</ check and create Directory >--
 
 
//DirectoryInfo subFolder = folder.GetDirectories(sDate, SearchOption.TopDirectoryOnly);
//if (subFolder)
//----</ File >----
}
//------</ @Loop: Files >------
}
//--------</ Folder >--------
//----</ Selected Folder >----

 
Mobile
»
WPF: Read string arguments with spaces
»
WPF: Additional menu items with icon in file explorer for videos, photos, folders and directories
»
WPF: Get all drives of a Computer
»
WPF: Hide hidden folders
»
WPF, C #: Move files With FileInfo.MoveTo
»
WPF: Get all SubDirectories in a Folder with matching Wildcard Pattern
»
WPF: Checking and creating folders

.

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