轉http://skybirdzw.blog.163.com/blog/static/7257062620099751329403/
文件處理-Directory類 (C#) ?
Directory.CreateDirectory 方法:創建指定路徑中的所有目錄。
Directory.CreateDirectory (String) 按 path 的指定創建所有目錄和子目錄。
Directory.CreateDirectory (String, DirectorySecurity) 創建指定路徑中的所有目錄,并應用指定的 Windows 安全性。
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? // Specify the directory you want to manipulate.
??????? string path = @"c:\MyDir\MyDoc";
??????? try
??????? {
??????????? // Determine whether the directory exists.
??????????? if (Directory.Exists(path))
??????????? {
??????????????? Console.WriteLine("That path exists already.");
??????????????? return;
??????????? }
??????????? // Try to create the directory.
??????????? DirectoryInfo di = Directory.CreateDirectory(path);
??????????? Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
??????????? // Delete the directory.
??????????? //di.Delete();
??????????? Console.WriteLine("The directory was deleted successfully.");
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??????? finally { }
??? }
}
//***************endCode**************************************
Directory.Delete 方法:刪除指定的目錄。
Directory.Delete (String) 從指定路徑刪除空目錄。
Directory.Delete (String, Boolean) 刪除指定的目錄并(如果指示)刪除該目錄中的任何子目錄。
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? // Specify the directories you want to manipulate.
??????? string path = @"c:\MyDir";
??????? string subPath = @"c:\MyDir\temp";
??????? try
??????? {
??????????? // Determine whether the directory exists.
??????????? if (!Directory.Exists(path))
??????????? {
??????????????? // Create the directory.
??????????????? Directory.CreateDirectory(path);
??????????? }
??????????? if (!Directory.Exists(subPath))
??????????? {
??????????????? // Create the directory.
??????????????? Directory.CreateDirectory(subPath);
??????????? }
??????????? // This will succeed because subdirectories are being deleted.
??????????? Console.WriteLine("I am about to attempt to delete {0}", path);
??????????? Directory.Delete(path, true);
??????????? Console.WriteLine("The Delete operation was successful.");
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??????? finally { }
??? }
}
//***************endCode**************************************
Directory.Exists 方法:確定給定路徑是否引用磁盤上的現有目錄。
Directory.GetCreationTime 方法:獲取目錄的創建日期和時間。
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? try
??????? {
??????????? // Get the creation time of a well-known directory.
??????????? DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);
??????????? // Give feedback to the user.
??????????? if (DateTime.Now.Subtract(dt).TotalDays > 364)
??????????? {
??????????????? Console.WriteLine("This directory is over a year old.");
??????????? }
??????????? else if (DateTime.Now.Subtract(dt).TotalDays > 30)
??????????? {
??????????????? Console.WriteLine("This directory is over a month old.");
??????????? }
??????????? else if (DateTime.Now.Subtract(dt).TotalDays <= 1)
??????????? {
??????????????? Console.WriteLine("This directory is less than a day old.");
??????????? }
??????????? else
??????????? {
??????????????? Console.WriteLine("This directory was created on {0}", dt);
??????????? }
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??? }
}
//***************endCode**************************************
Directory.GetCurrentDirectory 方法:獲取應用程序的當前工作目錄。
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? try
??????? {
??????????? // Get the current directory.
??????????? string path = Directory.GetCurrentDirectory();
??????????? string target = @"c:\temp";
??????????? Console.WriteLine("The current directory is {0}", path);
??????????? if (!Directory.Exists(target))
??????????? {
??????????????? Directory.CreateDirectory(target);
??????????? }
??????????? // Change the current directory.
??????????? Environment.CurrentDirectory = (target);
??????????? if (path.Equals(Directory.GetCurrentDirectory()))
??????????? {
??????????????? Console.WriteLine("You are in the temp directory.");
??????????? }
??????????? else
??????????? {
??????????????? Console.WriteLine("You are not in the temp directory.");
??????????? }
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??? }
}
//***************endCode**************************************
Directory.GetDirectories 方法:獲取指定目錄中子目錄的名稱。
Directory.GetDirectories (String) 獲取指定目錄中子目錄的名稱。
Directory.GetDirectories (String, String) 從當前目錄獲取與指定搜索模式匹配的目錄的數組。
Directory.GetDirectories (String, String, SearchOption) 獲取當前目錄中與指定搜索模式匹配并使用某個值確定是否在子目錄中搜索的目錄的數組。
//***************codeTest**************************************
using System;
using System.IO;
class SubDir
{
??? public static void Main()
??? {
??????? string path = @"F:\課件\通信原理";
??????? try
??????? {
??????????? string[] dirs = Directory.GetDirectories(path);
??????????? Console.WriteLine("F:\\課件\\通信原理 子目錄數 = {0}", dirs.Length);
??????????? foreach (string dir in dirs)
??????????????? Console.WriteLine(dir);
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("失敗: {0}", e.ToString());
??????? }
??? }
}
//***************endCode**************************************
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? try
??????? {
??????????? // Only get subdirectories that begin with the letter "p."
??????????? string[] dirs = Directory.GetDirectories(@"c:\", "p*");
??????????? Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
??????????? foreach (string dir in dirs)
??????????? {
??????????????? Console.WriteLine(dir);
??????????? }
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??? }
}
//***************endCode**************************************
Directory.GetFiles 方法:返回指定目錄中的文件的名稱。
Directory.GetFiles (String) 返回指定目錄中的文件的名稱。
Directory.GetFiles (String, String) 返回指定目錄中與指定搜索模式匹配的文件的名稱。
Directory.GetFiles (String, String, SearchOption) 返回指定目錄中文件的名稱,該目錄與指定搜索模式匹配并使用某個值確定是否在子目錄中搜索。
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? try
??????? {
??????????? // Only get files that begin with the letter "c."
??????????? string[] dirs = Directory.GetFiles(@"F:\memo","*.txt");
??????????? Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
??????????? foreach (string dir in dirs)
??????????? {
??????????????? Console.WriteLine(dir);
??????????? }
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??? }
}
//***************endCode**************************************
Directory.GetFileSystemEntries 方法:返回指定目錄中所有文件和子目錄的名稱。
Directory.GetFileSystemEntries (String) 返回指定目錄中所有文件和子目錄的名稱。
Directory.GetFileSystemEntries (String, String) 返回與指定搜索條件匹配的文件系統項的數組。
Directory.Move 方法:將文件或目錄及其內容移到新位置。
//***************codeTest**************************************
using System;
using System.IO;
class Test
{
??? public static void Main()
??? {
??????? try
??????? {
??????????? string path = @"F:\memo";
??????????? string path2 = @"F:\VC++串口通訊\memo";
??????????? Directory.Move(path, path2);
??????? }
??????? catch (Exception e)
??????? {
??????????? Console.WriteLine("The process failed: {0}", e.ToString());
??????? }
??? }
}
//***************endCode**************************************
舉 例來講,如果您嘗試將 c:\mydir 移到 c:\public,并且 c:\public 已存在,則此方法引發 IOException。您必須將“c:\\public\\mydir”指定為 destDirName 參數(假設“c:\\public”下不存在“mydir”),或者指定一個新的目錄名,例如“c:\\newdir”。
//***************codeTest**************************************
using System;
namespace GetFileSystemEntries
{
??? class Class1
??? {
??????? static void Main(string[] args)
??????? {
??????????? Class1 snippets = new Class1();
??????????? string path = System.IO.Directory.GetCurrentDirectory();
??????????? string filter = "*.exe";
??????????? snippets.PrintFileSystemEntries(path);
??????????? snippets.PrintFileSystemEntries(path, filter);
??????????? snippets.GetLogicalDrives();
??????????? snippets.GetParent(path);
??????????? snippets.Move("C:\\proof", "C:\\Temp");
??????? }
??????? void PrintFileSystemEntries(string path)
??????? {
??????????? try
??????????? {
??????????????? // Obtain the file system entries in the directory path.
??????????????? string[] directoryEntries =
??????????????????? System.IO.Directory.GetFileSystemEntries(path);
??????????????? foreach (string str in directoryEntries)
??????????????? {
??????????????????? System.Console.WriteLine(str);
??????????????? }
??????????? }
??????????? catch (ArgumentNullException)
??????????? {
??????????????? System.Console.WriteLine("Path is a null reference.");
??????????? }
??????????? catch (System.Security.SecurityException)
??????????? {
??????????????? System.Console.WriteLine("The caller does not have the " +
??????????????????? "required permission.");
??????????? }
??????????? catch (ArgumentException)
??????????? {
??????????????? System.Console.WriteLine("Path is an empty string, " +
??????????????????? "contains only white spaces, " +
??????????????????? "or contains invalid characters.");
??????????? }
??????????? catch (System.IO.DirectoryNotFoundException)
??????????? {
??????????????? System.Console.WriteLine("The path encapsulated in the " +
??????????????????? "Directory object does not exist.");
??????????? }
??????? }
??????? void PrintFileSystemEntries(string path, string pattern)
??????? {
??????????? try
??????????? {
??????????????? // Obtain the file system entries in the directory
??????????????? // path that match the pattern.
??????????????? string[] directoryEntries =
??????????????????? System.IO.Directory.GetFileSystemEntries(path, pattern);
??????????????? foreach (string str in directoryEntries)
??????????????? {
??????????????????? System.Console.WriteLine(str);
??????????????? }
??????????? }
??????????? catch (ArgumentNullException)
??????????? {
??????????????? System.Console.WriteLine("Path is a null reference.");
??????????? }
??????????? catch (System.Security.SecurityException)
??????????? {
??????????????? System.Console.WriteLine("The caller does not have the " +
??????????????????? "required permission.");
??????????? }
??????????? catch (ArgumentException)
??????????? {
??????????????? System.Console.WriteLine("Path is an empty string, " +
??????????????????? "contains only white spaces, " +
??????????????????? "or contains invalid characters.");
??????????? }
??????????? catch (System.IO.DirectoryNotFoundException)
??????????? {
??????????????? System.Console.WriteLine("The path encapsulated in the " +
??????????????????? "Directory object does not exist.");
??????????? }
??????? }
??????? // Print out all logical drives on the system.
??????? void GetLogicalDrives()
??????? {
??????????? try
??????????? {
??????????????? string[] drives = System.IO.Directory.GetLogicalDrives();
??????????????? foreach (string str in drives)
??????????????? {
??????????????????? System.Console.WriteLine(str);
??????????????? }
??????????? }
??????????? catch (System.IO.IOException)
??????????? {
??????????????? System.Console.WriteLine("An I/O error occurs.");
??????????? }
??????????? catch (System.Security.SecurityException)
??????????? {
??????????????? System.Console.WriteLine("The caller does not have the " +
??????????????????? "required permission.");
??????????? }
??????? }
??????? void GetParent(string path)
??????? {
??????????? try
??????????? {
??????????????? System.IO.DirectoryInfo directoryInfo =
??????????????????? System.IO.Directory.GetParent(path);
??????????????? System.Console.WriteLine(directoryInfo.FullName);
??????????? }
??????????? catch (ArgumentNullException)
??????????? {
??????????????? System.Console.WriteLine("Path is a null reference.");
??????????? }
??????????? catch (ArgumentException)
??????????? {
??????????????? System.Console.WriteLine("Path is an empty string, " +
??????????????????? "contains only white spaces, or " +
??????????????????? "contains invalid characters.");
??????????? }
??????? }
??????? void Move(string sourcePath, string destinationPath)
??????? {
??????????? try
??????????? {
??????????????? System.IO.Directory.Move(sourcePath, destinationPath);
??????????????? System.Console.WriteLine("The directory move is complete.");
??????????? }
??????????? catch (ArgumentNullException)
??????????? {
??????????????? System.Console.WriteLine("Path is a null reference.");
??????????? }
??????????? catch (System.Security.SecurityException)
??????????? {
??????????????? System.Console.WriteLine("The caller does not have the " +
??????????????????? "required permission.");
??????????? }
??????????? catch (ArgumentException)
??????????? {
??????????????? System.Console.WriteLine("Path is an empty string, " +
??????????????????? "contains only white spaces, " +
??????????????????? "or contains invalid characters.");
??????????? }
??????????? catch (System.IO.IOException)
??????????? {
??????????????? System.Console.WriteLine("An attempt was made to move a " +
??????????????????? "directory to a different " +
??????????????????? "volume, or destDirName " +
??????????????????? "already exists.");
??????????? }
??????? }
??? }
}
//***************endCode**************************************
Notice: Codes were mainly from MSDN
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
