Example from Troelsen
everywhere using SystemIO;
1 2 3 4 5 6 7 8 9 10 11 12 |
static void ShowWindowsDirectoryInfo() { DirectoryInfo dir = new DirectoryInfo(@"C:\WIndows"); Console.WriteLine("Fullname {0}",dir.FullName); Console.WriteLine("Name {0}", dir.Name); Console.WriteLine("Parent {0}", dir.Parent); Console.WriteLine("Creation {0}", dir.CreationTime); Console.WriteLine("Attributes {0}", dir.Attributes); Console.WriteLine("Root {0}", dir.Root); } |
Enum image files example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static void DisplayImageFiles() { DirectoryInfo dir = new DirectoryInfo(@"C:\Windows\Web\Wallpaper"); // getting all files jpg FileInfo[] imagefiles = dir.GetFiles("*.jpg",SearchOption.AllDirectories); // count Console.WriteLine("Found {0} *.jpg files\n",imagefiles.Length); foreach (FileInfo f in imagefiles) { Console.WriteLine("File name {0}", f.Name); Console.WriteLine("File size {0}", f.Length); Console.WriteLine("Creation {0}", f.CreationTime); Console.WriteLine("Attributes {0}", f.Attributes); Console.WriteLine("***\n"); } } |
Create subdirs example
1 2 3 4 5 6 7 |
static void CreateSubDirs() { DirectoryInfo dir = new DirectoryInfo(@"C:\"); dir.CreateSubdirectory("MyFolder"); DirectoryInfo subdir = dir.CreateSubdirectory(@"MyFolder\Data"); Console.WriteLine("New folder is {0}: ", subdir); } |
Msdn example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.IO; class Test { public static void Main() { // Specify the directories you want to manipulate. DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); try { // Determine whether the directory exists. if (di.Exists) { // Indicate that the directory already exists. Console.WriteLine("That path exists already."); return; } // Try to create the directory. di.Create(); Console.WriteLine("The directory was created successfully."); // Delete the directory. di.Delete(); Console.WriteLine("The directory was deleted successfully."); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally {} } } |
Another one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
using System; using System.IO; class CopyDir { public static void CopyAll(DirectoryInfo source, DirectoryInfo target) { if (source.FullName.ToLower() == target.FullName.ToLower()) { return; } // Check if the target directory exists, if not, create it. if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); } // Copy each file into it's new directory. foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); CopyAll(diSourceSubDir, nextTargetSubDir); } } public static void Main() { string sourceDirectory = @"c:\sourceDirectory"; string targetDirectory = @"c:\targetDirectory"; DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); CopyAll(diSource, diTarget); } // Output will vary based on the contents of the source directory. } |