個人可以做淘寶客網(wǎng)站嗎/網(wǎng)絡(luò)營銷首先要進(jìn)行
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請在顯著位置標(biāo)明本文出處以及作者網(wǎng)名,未經(jīng)作者允許不得用于商業(yè)目的。
10.2.1 DirectoryInfo類
DirectoryInfo類可以獲得目錄信息。
DirectoryInfo常用屬性:
- Name:獲取DirectoryInfo實例的名稱,例如:c:\windows\system32將返回system32。
- FullName:獲取目錄或文件的完整目錄。例如:c:\windows\system32將返回c:\windows\system32。
- Attributes:目錄的屬性。
- Exists:目錄是否存在。
- CreationTime:目錄的創(chuàng)建時間。
- LastWriteTime:目錄的最后修改時間。
- Parent:目錄的父目錄。
DirectoryInfo常用方法:
- Create:創(chuàng)建目錄。
- Delete:刪除目錄,使用此方法帶參數(shù)的重載可以實現(xiàn)刪除目錄下的子目錄和文件,否則只能刪除空目錄。
- MoveTo:移動目錄。注意:此方法只能在同一個磁盤分區(qū)下移動。
- GetDirectories:返回當(dāng)前目錄的子目錄列表,這是一個DirectoryInfo數(shù)組。
- GetFiles:返回當(dāng)前目錄的文件列表,這是一個FileInfo數(shù)組。關(guān)于FileInfo類,請參看第10.3.1節(jié)。
10.2.1.1 獲取目錄信息
【例 10.2】【項目:code10-002】獲得計算機(jī)上某個目錄的信息。
當(dāng)選擇某個文件夾后,立即在文本框中顯示該文件夾的相關(guān)信息:
???? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? fbd.ShowNewFolderButton = false;
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? string folderpath = fbd.SelectedPath;
??????????? DirectoryInfo di = new DirectoryInfo(folderpath);
??????????? textBox1.Text = "文件夾:" + di.Name + "\r\n";
??????????? textBox1.Text += "路徑:" + di.FullName + "\r\n";
??????????? textBox1.Text += "創(chuàng)建日期:" + di.CreationTime.ToString("yyyy-MM-dd") + "\r\n";
??????????? textBox1.Text += "最后修改日期:" + di.LastWriteTime.ToString("yyyy-MM-dd") + "\r\n";
??????????? textBox1.Text += "屬性:" + getFolderAttr(di.Attributes) + "\r\n";
??????? }
??????? //獲得文件夾屬性
??????? private string getFolderAttr(FileAttributes attr)
??????? {
??????????? string strAttr = "";
??????????? if((attr & FileAttributes.Archive)==FileAttributes.Archive)
??????????????? strAttr += " 備份";
??????????? if ((attr & FileAttributes.Hidden) == FileAttributes.Hidden)
??????????????? strAttr += " 隱藏";
??????????? if ((attr & FileAttributes.Compressed) == FileAttributes.Compressed)
??????????????? strAttr += " 壓縮";
??????????? if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
??????????????? strAttr += " 目錄";
??????????? if ((attr & FileAttributes.Encrypted) == FileAttributes.Encrypted)
??????????????? strAttr += " 加密";
??????????? if ((attr & FileAttributes.Offline) == FileAttributes.Offline)
??????????????? strAttr += " 脫機(jī)";
??????????? if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
??????????????? strAttr += " 只讀";
? ??????????if ((attr & FileAttributes.System) == FileAttributes.System)
??????????????? strAttr += " 系統(tǒng)";
??????????? if ((attr & FileAttributes.Temporary) == FileAttributes.Temporary)
??????????????? strAttr += " 臨時";
??????????? return strAttr;
??????? }
運行結(jié)果如下圖所示:
圖10-2 獲取目錄信息
10.2.1.2 獲取子目錄和文件
獲取當(dāng)前目錄的子目錄和所有文件主要是使用GetDirectories和GetFiles方法,這兩個方法返回包含的子目錄(DirectoryInfo數(shù)組)和文件(FileInfo數(shù)組),只需要分別遍歷這兩個數(shù)組即可獲得相關(guān)信息。
【例 10.3】【項目:code10-003】獲得子目錄和文件。
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? label1.Text = fbd.SelectedPath;
??????????? DirectoryInfo di = new DirectoryInfo(label1.Text);
??????????? listBox1.Items.Clear();
??????????? //遍歷當(dāng)前目錄下的子目錄
??????????? foreach (DirectoryInfo subdi in di.GetDirectories())
??????????? {
??????????????? listBox1.Items.Add("目錄:" + subdi.Name);
??????????? }
??????????? //遍歷當(dāng)前目錄下的文件
???? ???????foreach (FileInfo fi in di.GetFiles())
??????????? {
??????????????? listBox1.Items.Add("文件:" + fi.Name);
??????????? }
??????? }
運行結(jié)果如下圖所示:
圖10-3 獲得選定目錄的子目錄和文件
10.2.1.3 設(shè)置目錄屬性
通過DirectoryInfo類的Attributes屬性可以獲得和設(shè)置目錄屬性。Attributes的值是一個FileAttributes枚舉,它提供了文件和目錄的屬性。
表10-1 FileAttributes枚舉常用成員及對應(yīng)屬性說明
成員名稱 | 對應(yīng)屬性說明 | 成員名稱 | 對應(yīng)屬性說明 |
Archive | 備份 | Normal | 正常 |
Compressed | 壓縮 | Offline | 脫機(jī) |
Directory | 指示這是目錄 | ReadOnly | 只讀 |
Encrypted | 加密 | System | 系統(tǒng) |
Hidden | 隱藏 | Temporary | 臨時 |
判斷現(xiàn)有屬性是否包括某個屬性,請使用:現(xiàn)有屬性 And 某個屬性:
表10-2 判斷是否包含只讀屬性
屬性 | 十進(jìn)制值 | 操作 | 二進(jìn)制值 | 十六進(jìn)制值 | |||||||
N+R | 129 | & (And) | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 81 |
ReadOnly | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | |
ReadOnly | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
現(xiàn)有屬性基礎(chǔ)上增加某個屬性,請使用:現(xiàn)有屬性 Or 某個屬性:
表10-3 增加隱藏屬性
屬性 | 十進(jìn)制值 | 操作 | 二進(jìn)制值 | 十六進(jìn)制值 | |||||||
Normal | 128 | | (Or) | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 80 |
Hidden | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2 | |
N+H | 130 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 82 |
從現(xiàn)有屬性中排除某個屬性,請使用:現(xiàn)有屬性 Xor 某個屬性:
表10-4 排除只讀屬性
屬性 | 十進(jìn)制值 | 操作 | 二進(jìn)制值 | 十六進(jìn)制值 | |||||||
N+R | 129 | ^ (Xor) | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 81 |
ReadOnly | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | |
Normal | 128 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 80 |
注意:微軟提供的很多Api常量和.Net常量都類似于FileAttributes常量枚舉,即按照 0、1、2、4、8……這樣定義的,很方便使用&、|、^等位運算符。
【例 10.4】【項目:code10-004】設(shè)置目錄屬性。
??????? //選擇目錄,并獲得其屬性
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? label1.Text = fbd.SelectedPath;
??????????? DirectoryInfo di = new DirectoryInfo(label1.Text);
??????????? //目錄有只讀屬性,那么勾選CheckBox1
??????????? if ((di.Attributes & FileAttributes.ReadOnly) ==FileAttributes.ReadOnly )
??????????????? checkBox1.Checked = true;
??????????? //目錄有隱藏屬性,那么勾選CheckBox2
??????????? if ((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
??????????????? checkBox2.Checked = true;
??????????? //目錄有系統(tǒng)屬性,那么勾選CheckBox3
??? ????????if ((di.Attributes & FileAttributes.System) == FileAttributes.System)
??????????????? checkBox3.Checked = true;
??????? }
??????? //設(shè)置指定目錄的屬性
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? DirectoryInfo di = new DirectoryInfo(label1.Text);
??????????? if (di.Exists == false)
??????????????? return;
??????????? FileAttributes fa = di.Attributes;
??????????? if ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
??????????? {
??????????????? //如果有只讀屬性,而設(shè)置取消只讀,那么使用異或方式
??????????????? if (checkBox1.Checked == false)
??????????????????? fa = fa ^ FileAttributes.ReadOnly;
??????????? }
?????????? else
??????????? {
??????????????? //如果沒有只讀屬性,而設(shè)置只讀,那么使用或方式
??????????????? if (checkBox1.Checked == true)
????? ??????????????fa = fa | FileAttributes.ReadOnly;
??????????? }
??????????? if ((fa & FileAttributes.Hidden) == FileAttributes.Hidden)
??????????? {
??????????????? //如果有隱藏屬性,而設(shè)置取消隱藏,那么使用異或方式
??????????????? if (checkBox2.Checked == false)
??????????????????? fa = fa ^ FileAttributes.Hidden;
??????????? }
??????????? else
??????????? {
??????????????? //如果沒有隱藏屬性,而設(shè)置隱藏,那么使用或方式
??????????????? if (checkBox2.Checked == true)
??????????????????? fa = fa | FileAttributes.Hidden;
??????????? }
??????????? if ((fa & FileAttributes.System) == FileAttributes.System)
??????????? {
??????????????? //如果有系統(tǒng)屬性,而設(shè)置取消系統(tǒng),那么使用異或方式
??????????????? if (checkBox3.Checked == false)
??????????????????? fa = fa ^ FileAttributes.System;
??????????? }
??????????? else
??????????? {
??????????????? //如果沒有系統(tǒng)屬性,而設(shè)置系統(tǒng),那么使用或方式
??????????????? if (checkBox3.Checked == true)
??????????????????? fa = fa | FileAttributes.System;
??????????? }
??????????? //目錄設(shè)置新屬性
??????????? di.Attributes = fa;
??????? }
運行結(jié)果如下圖所示:
圖10-4 設(shè)置目錄屬性并使用attrib命令驗證
10.2.1.4 目錄的創(chuàng)建、刪除和移動
目錄的創(chuàng)建、刪除和移動分別使用到了DirectoryInfo類的Create、Delete和MoveTo方法。
【例 10.5】【項目:code10-005】目錄的創(chuàng)建、刪除和移動。
??????? //獲得源目錄路徑
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? label1.Text = fbd.SelectedPath;
??????? }
??????? //獲得目標(biāo)目錄路徑
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? label2.Text = fbd.SelectedPath;
??????? }
??????? //在源目錄下創(chuàng)建一個子目錄 csharp
??????? private void button3_Click(object sender, EventArgs e)
??????? {
??????????? if (label1.Text == "")
??????????????? return;
??????????? string newFolder = label1.Text.Substring(label1.Text.Length - 1, 1) == "\\" ?
??????????????? label1.Text.Substring(0, label1.Text.Length - 1) :
??????????????? label1.Text;
??????????? DirectoryInfo di = new DirectoryInfo(newFolder + "\\csharp");
??????????? if(di.Exists )
??????????? {
??????????????? MessageBox.Show("目錄已經(jīng)存在,不能創(chuàng)建該目錄。", "錯誤信息");
??????????????? return;
??????????? }
??????????? di.Create();
??????????? MessageBox.Show("目錄創(chuàng)建成功。", "提示信息");
??????? }
??????? //刪除源目錄
??????? private void button4_Click(object sender, EventArgs e)
??????? {
??????????? if (label1.Text == "")
??????????????? return;
??????????? DirectoryInfo di = new DirectoryInfo(label1.Text );
??????????? if (di.Parent == null)
??????????? {
??????????????? MessageBox.Show("為確保文件安全,不允許刪除根目錄", "錯誤信息");
??????????????? return;
??????????? }
??????????? if(di.Exists )
??????????? {
??????????????? try
??????????????? {
??????????????????? if (MessageBox.Show("確實要刪除此目錄?", "警告信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
??????????????????????? return;
??????????????????? di.Delete(true);
??????????????????? MessageBox.Show("目錄已經(jīng)成功刪除。", "提示信息");
??????????????? }
??????????????? catch(Exception ex)
??????????????? {
??????????????????? MessageBox.Show("刪除目錄時發(fā)生錯誤:" + ex.Message, "錯誤信息");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? MessageBox.Show("目錄不存在。", "錯誤信息");
??????????? }
??????? }
??????? //移動源目錄到目標(biāo)目錄,注意:必須在同一分區(qū)下移動
??????? private void button5_Click(object sender, EventArgs e)
??????? {
??????????? if (label1.Text == "" | label2.Text == "")
??????????????? return;
??????????? DirectoryInfo di = new DirectoryInfo(label1.Text);
??????????? string toFolder;
??????????? toFolder = label2.Text.Substring(label2.Text.Length - 1, 1) == "\\" ? label2.Text : label2.Text + "\\";
??????????? toFolder = toFolder + di.Name;
??????????? if(di.Exists)
??????????? {
??????????????? try
??????????????? {
??????????????????? di.MoveTo(toFolder);
??????????????????? MessageBox.Show("目錄移動完成。", "提示信息");
??????????????? }
????????????? ??catch(Exception ex)
??????????????? {
??????????????????? MessageBox.Show("移動目錄時發(fā)生錯誤:"+ ex.Message, "錯誤信息");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? MessageBox.Show("目錄不存在。", "錯誤信息");
??????????? }
??????? }
運行結(jié)果如下圖所示:
圖10-5目錄的創(chuàng)建、刪除和移動
10.2.2 Directory類
除了DirictoryInfo類外,VB.Net還提供了Directory類來操作目錄、獲得目錄信息。
Directory類與DirectoryInfo類相比,Directory沒有屬性,提供的方法都是靜態(tài)方法,可以直接使用;而DirectoryInfo的方法需要類實例化后使用。
其實DirectoryInfo的一些方法甚至就是調(diào)用Directory對應(yīng)的方法。
Directory常用方法:
- CreateDirectory:創(chuàng)建目錄。
- Delete:刪除目錄。
- Move:移動目錄。
- GetLogicalDrives:檢索此計算機(jī)上格式為“<驅(qū)動器號>:\”的邏輯驅(qū)動器的名稱,這是一個字符串?dāng)?shù)組。
- GetDirectories:子目錄的名稱(包括其路徑),這是一個字符串?dāng)?shù)組。
- GetFiles:目錄中文件的名稱(包括其路徑),這是一個字符串?dāng)?shù)組。
- GetCreationTime:獲得目錄創(chuàng)建時間。
- GetLastWriteTime:獲得目錄最后修改時間。
- GetLastAccessTime:獲得目錄最后訪問時間。
- GetParent:獲得父目錄,這是一個DirectoryInfo類型。
- SetCreationTime:設(shè)置目錄創(chuàng)建時間。
- SetLastWriteTime:設(shè)置目錄最后修改時間。
- SetLastAccessTime:設(shè)置目錄最后訪問時間。
- GetCurrentDirecotry:獲得程序所在路徑。
- Exists:目錄是否存在。
10.2.2.1 獲取目錄信息
【例 10.6】【項目:code10-006】獲取某個目錄的信息。
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? fbd.ShowNewFolderButton = false;
??????????? if (fbd.ShowDialog() !=DialogResult.OK)
??????????????? return;
??????????? string folderPath = fbd.SelectedPath;
??????????? textBox1.Text += "路徑:" + folderPath + "\r\n";
??????????? textBox1.Text += "根目錄:" + Directory.GetDirectoryRoot(folderPath) + "\r\n";
??????????? if (Directory.GetParent(folderPath)!=null)
??????????????? textBox1.Text += "父目錄:" + Directory.GetParent(folderPath).FullName + "\r\n";???????????
??????????? textBox1.Text += "創(chuàng)建日期:" + Directory.GetCreationTime(folderPath).ToString("yyyy-MM-dd") + "\r\n";
??????????? textBox1.Text += "最后修改日期:" + Directory.GetLastWriteTime(folderPath).ToString("yyyy-MM-dd") + "\r\n";
??????????? textBox1.Text += "最后訪問日期:" + Directory.GetLastAccessTime(folderPath).ToString("yyyy-MM-dd");
??????? }
運行結(jié)果如下圖所示:
圖10-6 獲取目錄信息
10.2.2.2 獲取子目錄和文件
獲取當(dāng)前目錄的子目錄和所有文件主要是使用GetDirectories和GetFiles方法,這兩個方法不同于DirectoryInfo類提供的方法,它們返回的是包含的子目錄和文件的全路徑的字符串?dāng)?shù)組。
【例 10.7】【項目:code10-007】獲得子目錄和文件。
???????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? listBox1.Items.Clear();
??????????? string selPath = fbd.SelectedPath;
??????????? //遍歷所有的子目錄
??????????? foreach ( string subdirectory in Directory.GetDirectories(selPath))
??????????? {
?????????????? ?listBox1.Items.Add("目錄" + subdirectory);
??????????? }
??????????? //遍歷所有的文件
??????????? foreach (string file in Directory.GetFiles(selPath))
??????????? {
??????????????? listBox1.Items.Add("文件" + file);
??????????? }
??????? }
運行結(jié)果同【例 10.3】。
10.2.2.3 目錄的創(chuàng)建、刪除和移動
目錄的創(chuàng)建、刪除和移動分別使用到了Directory類的CreateDirectory、Delete和Move方法。
【例 10.8】【項目:code10-008】目錄的創(chuàng)建、刪除和移動。
??????? //獲得源目錄路徑
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? label1.Text = fbd.SelectedPath;
??????? }
??????? //獲得目標(biāo)目錄路徑
??????? private void button2_Click(object sender, EventArgs e)
??????? {
???????? ???FolderBrowserDialog fbd = new FolderBrowserDialog();
??????????? if (fbd.ShowDialog() != DialogResult.OK)
??????????????? return;
??????????? label2.Text = fbd.SelectedPath;
??????? }
??????? //在源目錄下創(chuàng)建一個子目錄 csharp
??????? private void button3_Click(object sender, EventArgs e)
??????? {
??????????? string newFolder = label1.Text.Substring(label1.Text.Length - 1, 1) == "\\" ? label1.Text.Substring(0, label1.Text.Length - 1) : label1.Text;
??????????? newFolder += "\\csharp";
??????????? if (Directory.Exists(newFolder))
??????????? {
??????????????? MessageBox.Show("目錄已經(jīng)存在,不能創(chuàng)建該目錄。", "錯誤信息");
??????????????? return;
??????????? }
??????????? Directory.CreateDirectory(newFolder);
??????????? MessageBox.Show("目錄創(chuàng)建成功。", "提示信息");
??????? }
??????? //刪除源目錄
??????? private void button4_Click(object sender, EventArgs e)
??????? {
??????????? if (label1.Text == "")
??????????????? return;
??????????? string folder = label1.Text;
??????????? if (Directory.GetParent(folder) == null)
??????????????? MessageBox.Show("為確保文件安全,不允許刪除根目錄", "錯誤信息");
??????????????? return;
??????????? if(Directory.Exists (folder))
??????????? {
??????????????? try
??????????????? {
??????????????????? if (MessageBox.Show("確實要刪除此目錄?", "警告信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
??????????????????????? return;
??????????????????? //調(diào)用Delete方法刪除,參數(shù)True表示要刪除該目錄中的子目錄和文件
??????????????????? Directory.Delete(folder, true);
??????????????????? //如果使用不帶參數(shù)的方法只能是刪除空目錄
??????????????????? Directory.Delete(folder);
??????????????????? MessageBox.Show("目錄已經(jīng)成功刪除。", "提示信息");
??????????????? }
??????????????? catch(Exception ex)
??????????????? {
??????????????????? MessageBox.Show("刪除目錄時發(fā)生錯誤:" + ex.Message, "錯誤信息");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? MessageBox.Show("目錄不存在。", "錯誤信息");
??????????? }
??????? }
???????? //移動源目錄到目標(biāo)目錄
???????? //注意必須在同一分區(qū)下移動
??????? private void button5_Click(object sender, EventArgs e)
??????? {
??????????? if (label1.Text == "" | label2.Text == "")
??????????????? return;
??????????? DirectoryInfo di = new DirectoryInfo(label1.Text);
??????????? string toFolder = label2.Text.Substring(label2.Text.Length - 1, 1) == "\\" ? label2.Text : label2.Text + "\\";
??????????? toFolder += di.Name;
??????????? if(Directory.Exists(label1.Text))
??????????? {
??????????????? try
??????????????? {
??????????????????? //調(diào)用MoveTo方法移動
??????????????????? Directory.Move(label1.Text, toFolder);
??????????????????? MessageBox.Show("目錄移動完成。", "提示信息");
??????????????? }
??????????????? catch (Exception ex)
??????????????? {
??????????????????? MessageBox.Show("移動目錄時發(fā)生錯誤:" + ex.Message, "錯誤信息");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? MessageBox.Show("目錄不存在。", "錯誤信息");
??????????? }
??????? }
運行結(jié)果同【例 10.5】。
學(xué)習(xí)更多vb.net知識,請參看vb.net 教程 目錄
學(xué)習(xí)更多C#知識,請參看C#教程 目錄