亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

文件處理-Directory類 (C#)

系統 2532 0

轉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

文件處理-Directory類 (C#)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精选在线播放 | 国产成人精品一区二区仙踪林 | 亚洲一区免费看 | 国产成人综合自拍 | 你懂的国产精品 | 亚洲精品综合欧美一区二区三区 | 香蕉视频免费看 | 色偷偷亚洲第一成人综合网址 | 国产免费观看a大片的网站 国产免费精彩视频 | 4虎 影视 免费 | 亚洲国产成人久久一区久久 | 日日日日操| 国产成人精品一区二三区2022 | 日韩一级欧美一级毛片在 | 九九热视频精品在线 | 国产精品久久久久久久久久日本 | 四虎最新永久免费视频 | 久久99精品久久 | 天码毛片一区二区三区入口 | 香蕉亚洲 | 黄毛片 | 伊人tv| 久久伦理片 | 播放一级片| 免费性网站 | 国产精品热久久毛片 | 一区二区三区www | 国产亚洲福利精品一区 | 国产高清在线精品二区一 | 日日夜夜精品视频 | 国产―笫一页―浮力影院xyz | 成人另类 | 亚洲日韩中文字幕一区 | 久久久久综合网久久 | 99精品视频在线免费观看 | 5151四虎永久在线精品免费 | 国产成人精品曰本亚洲78 | 色综合夜夜嗨亚洲一二区 | 亚洲人和日本人hd | 最近中文日本字幕免费完整 | 免费观看性欧美毛片 |