using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
namespace DirectoryApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Directory(Info) *****\n");
ShowWindowsDirectoryInfo();
Console.ReadLine();
DisplayImageFiles();
Console.ReadLine();
ModifyAppDirectory();
Console.ReadLine();
FunWithDirectoryType();
Console.ReadLine();
}
#region Show basic directory info
static void ShowWindowsDirectoryInfo()
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Windows");
// Dump directory information.
Console.WriteLine("***** Directory Info *****");
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);
Console.WriteLine("**************************\n");
}
#endregion
#region Show info on image types
static void DisplayImageFiles()
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Windows\Web\Wallpaper");
// Get all files with a *.bmp extension.
FileInfo[] imageFiles = dir.GetFiles("*.jpg");
// How many were found?
Console.WriteLine("Found {0} *.jpg files\n", imageFiles.Length);
// Now print out info for each file.
foreach (FileInfo f in imageFiles)
{
Console.WriteLine("***************************");
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");
}
}
#endregion
#region Modify directory structure
static void ModifyAppDirectory()
{
DirectoryInfo dir = new DirectoryInfo(".");
// Create \MyFolder off initial directory.
dir.CreateSubdirectory("MyFolder");
// Capture returned DirectoryInfo object.
DirectoryInfo myDataFolder = dir.CreateSubdirectory(@"MyFolder2\Data");
// Prints path to ..\MyFolder2\Data.
Console.WriteLine("New Folder is: {0}", myDataFolder);
}
#endregion
#region Using Directory type
static void FunWithDirectoryType()
{
// List all drives on current computer.
string[] drives = Directory.GetLogicalDrives();
Console.WriteLine("Here are your drives:");
foreach (string s in drives)
Console.WriteLine("--> {0} ", s);
// Delete what was created.
Console.WriteLine("Press Enter to delete directories");
Console.ReadLine();
try
{
Directory.Delete(string.Format(@"{0}\MyFolder",
Environment.CurrentDirectory));
// The second parameter specifies if you
// wish to destroy any subdirectories.
Directory.Delete(string.Format(@"{0}\MyFolder2",
Environment.CurrentDirectory), true);
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
#endregion
}
}
转载于:https://my.oschina.net/duluo180/blog/8135