51
CH07 CH07

CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

  • View
    244

  • Download
    7

Embed Size (px)

Citation preview

Page 1: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

CH07CH07

Page 2: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

檔案存取檔案存取

Page 3: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念Visual Basic 2005 當中存取檔案的方式

•透過 System.IO.File ( 傳統方法 )

•透過 My.Computer.FileSystem (VB2005 新功能 )

'原有存取檔案資訊的方法 Dim dir As New IO.DirectoryInfo("C:\") Dim fi() As IO.FileInfo = dir.GetFiles("*.*") '取得 C槽底下檔案的完整路徑 Dim fullPathAr As New ArrayList For Each docPath As IO.FileInfo In fi fullPathAr.Add(docPath.FullName) Next '新的存取檔案路徑的方法 Dim ar As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchTopLevelOnly, "*.*")

從範例您可以發現,程式碼大幅的減少,開發人員所要進行的 Coding 時間相對的縮短,產量當然也就提高了。

Page 4: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.CopyDirectory()EX: CH07-02

'複製資料夾My.Computer.FileSystem.CopyDirectory("c:\test", "d:\files\testFile",

True)

Page 5: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.CreateDirectory()EX: CH07-03

'建立一個新的資料夾 My.Computer.FileSystem.CreateDirectory("C:\newDirectory\test")

Page 6: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.CurrentDirectory()EX: CH07-04

'取得目前資料夾位置 Dim curDir As String =

My.Computer.FileSystem.CurrentDirectory() MsgBox(curDir)

Page 7: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.DeleteDirectory()EX: CH07-05

'刪除資料夾,不管資料夾底下是否有其他檔案 My.Computer.FileSystem.DeleteDirectory("c:\test",

FileIO.DeleteDirectoryOption.DeleteAllContents)

'刪除資料夾,出現對話視窗讓使用者選擇是否要刪除資料夾 My.Computer.FileSystem.DeleteDirectory("c:\test",FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)

Page 8: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.DirectoryExists()EX: CH07-06

'查看資料夾是否存在 Dim dirFlag As Boolean =

My.Computer.FileSystem.DirectoryExists("c:\test") MsgBox(dirFlag)

判斷 C 槽下是否有 test 資料夾,如果 C 槽底下有 test 資料夾,則回傳 True ,如果 test 資料夾不存在則回傳 False 。

Page 9: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetDirectories()EX: CH07-07

'取得資料夾底下的子資料夾路徑集合 Dim subDir As

System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetDirectories("c:\test", FileIO.SearchOption.SearchTopLevelOnly, "*文件 ")

For Each dir As String In subDir MsgBox(dir) Next

搜尋在 c 槽的 test 資料夾底下,有沒有資料夾是以『文件』結尾的。如果 c 槽的 test 資料夾底下有『書籍文件、文件暫存區、資料文件檔』這三個資料夾,則會讀出『 c:\test\ 書籍文件』這個路徑。

Page 10: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetDirectoryInfo()EX: CH07-08

'取得資料夾訊息 Dim dirInfo As IO.DirectoryInfo =

My.Computer.FileSystem.GetDirectoryInfo("c:\test") MsgBox(dirInfo.CreationTime)

指令『 GetDirectoryInfo 』將會傳回指定的資料夾資訊,回傳值以『 DirectoryInfo 』物件呈現,上面的程式碼讀取 c 槽的 test 資料夾訊息,並回傳給 DirectoryInfo 物件 ( 名為 dirInfo) ,而『 dirInfo .CreationTime 』則是取回 test 資料夾的建立時間。

Page 11: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.MoveDirectory()EX: CH07-09

'移動資料夾,當目的地位置有相同名稱的資料夾,直接覆蓋掉 My.Computer.FileSystem.MoveDirectory("c:\test", "d:\files\

testFile", True)

'移動資料夾,當目的地位置有相同名稱的資料夾,出現對話視窗讓使用者選擇是否要覆蓋

My.Computer.FileSystem.MoveDirectory("c:\test", "d:\files\testFile", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)

程式碼將 c 槽底下的 test 資料夾移動到 d 槽的 files 的 testFile ,移動後 c 槽下面的 test 資料夾會被刪除。底下為兩種常用確認移動的方式,一種是直接設定當目的地已經存在同名資料夾,就直接覆蓋;另一種是出現對話視窗,讓使用者選擇是否要覆蓋。

Page 12: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.RenameDirectory()EX: CH07-10

'更改資料夾名稱My.Computer.FileSystem.RenameDirectory("c:\test", "BookExamples")

程式碼將 c 槽下面的 test 資料夾更名為『 BookExamples 』。

Page 13: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.SpecialDirectories()EX: CH07-11

'取得系統『 ProgramFiles』資料夾的路徑 Dim MyDocPath As String =

My.Computer.FileSystem.SpecialDirectories.ProgramFiles MsgBox(MyDocPath)

在系統當中,有一些特殊的常用資料夾,例如『我的文件』、『 Program Files 』…等。這些資料夾的位置並不一定是我們熟悉的 C:\… ,因為使用者可以將系統安裝在不同的硬碟 ( 例如: D:\) ,同時像『我的文件』這類的資料夾,還會因為該電腦目前登入的是不同的使用者而有不同的位置,因此該資料夾的位置可能不確定。

此函式之功能,則在傳回正確的資料夾位置,以讓我們在程式碼中可以取得正確的路徑加以運作。

Page 14: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.Drives()EX:取得電腦上磁碟的集合

'取得電腦上磁碟的集合 Dim disk As

System.Collections.ObjectModel.ReadOnlyCollection(Of System.IO.DriveInfo) = My.Computer.FileSystem.Drives

‘讀取磁碟機相關資訊 For Each obj As IO.DriveInfo In disk MsgBox(obj.Name & "的磁碟格式為: " & obj.DriveFormat) Next取得電腦上磁碟集合後,可以取得個別的磁碟訊息。每個人電腦的作業環境不同,因此有可能會讀取到不一樣的磁碟內容,筆者的作業環境中有三個磁碟,執行下面這一段程式碼時,第一筆會讀取到的磁碟資料為『 C:\ 』的磁碟格式為 FAT32 ;第二筆為『 D:\ 』的磁碟格式為: NTFS ;最後是 E 槽光碟機,在還沒有放入光碟片,會引發『裝置未就緒』的錯誤訊息。

Page 15: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetDriveInfo()EX:取得磁碟訊息

'取得磁碟機相關資訊 Dim driInfo As IO.DriveInfo =

My.Computer.FileSystem.GetDriveInfo("C:") MsgBox(driInfo.TotalSize)

DriveInfo 物件,可用來表達一個邏輯磁碟機的狀態,也可以透過 DriveInfo 物件的屬性來取得該磁碟機的可用空間等資訊。上面的程式碼為取得 c 槽磁碟訊息後,讀出 c槽可用空間的大小。 屬性名稱 說明

AvailableFreeSpace 指出磁碟上的可用空間量。DriveFormat 取得檔案系統的名稱,例如 NTFS 或 FAT32。DriveType 傳回磁碟類型。IsReady 取得值,指出磁碟是否就緒。Name 傳回磁碟名稱。RootDirectory 傳回磁碟的根目錄。TotalFreeSpace 傳回磁碟上可用空間的總量。TotalSize 傳回磁碟上儲存空間的總大小。VolumeLabel 傳回或設定磁碟的磁碟區標籤。

Page 16: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.CopyFile()EX:複製檔案

'複製檔案,設定當檔案名稱相同是否要覆寫檔案My.Computer.FileSystem.CopyFile("c:\test\book.txt", "d:\files\book.txt",

True)

'複製檔案,當檔案名稱相同,出現對話視窗讓使用者選擇是否要覆寫My.Computer.FileSystem.CopyFile("c:\test\book.txt", "d:\files\book.txt",

FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)

將 C 槽 test 資料夾底下的『 book.txt 』文字檔,複製到 D 槽 files 資料夾底下;當 D 槽下面沒有『 files 』資料夾時,程式會自動建立一個『 files 』資料夾,所以目的地資料夾不一定要存在。

Page 17: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.DeleteFile()EX: CH07-12

'刪除檔案My.Computer.FileSystem.DeleteFile("c:\test\book.txt")

'刪除檔案,出現對話視窗讓使用者選擇是否要刪除檔案 My.Computer.FileSystem.DeleteFile("c:\test\book.txt",FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)刪除 C 槽 test 資料夾底下的『 book.txt 』文字檔,上面兩種刪除的差異

在於,一種為直接刪除檔案,另一種會出現對話視窗讓使用者選擇是否要刪除檔案:

Page 18: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.FileExists()EX: CH07-13

'查看檔案是否存在 Dim fileFlag As Boolean = My.Computer.FileSystem.FileExists("c:\

test\book.txt") MsgBox(fileFlag)

判斷 C 槽 test 資料夾底下是否有『 book.txt 』檔案,如果 book.txt 存在,則回傳 True ,如果 book.txt 不存在則回傳 False 。

這個功能多半在我們想要從硬碟上開啟一個文字檔案 (或 Binary 格式的檔案 ) 、或是想要建立一個新的檔案時,先檢查該檔案是否存在。

事先檢查可以避免該檔案不存在時,我們直接開啟該檔案所發生的執行階段錯誤;或是同檔名的檔案已經存在,然後我們又企圖建立一個新的檔案時所發生的錯誤。

Page 19: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.FindInFiles()EX: CH07-14

'檢查檔案內文 Dim filePaths As

System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.FindInFiles("c:\test", "chapter7", False, FileIO.SearchOption.SearchTopLevelOnly)

MsgBox(filePaths.Count)查看 C 槽 test 資料夾底下,有沒有任何檔案的文字內容包含『 chapter7 』。如果有,則將其傳回。

目前我們測試過幾種檔案類型,包括Word 檔、 Excel 檔、 ppt 檔、 txt檔都可以正確讀取。

這是一個非常好用的函式,過去的 VB 並沒有提供,它提供了類似檔案總管的搜尋功能,可以幫我們輕鬆的完成全文檢索的功能。

Page 20: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetFileInfo()EX: CH07-15

'取得檔案訊息 Dim fileInfo As IO.FileInfo =

My.Computer.FileSystem.GetFileInfo("c:\test\book.txt") MsgBox(fileInfo.LastAccessTime)

取得 c 槽 test 資料夾底下的 book.txt 檔案訊息。上面程式碼中的『 LastAccessTime 』指的是檔案最後的存取時間。

程式碼中回傳的 FileInfo 物件,可用來表達一個檔案的資訊狀態,透過 FileInfo 物件可以用來取得該檔案的建立時間、大小…等資訊。

Page 21: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetFiles()EX: CH07-16

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'取得資料夾底下的所有圖檔名稱,放到 ComboBox Dim dirPath As String = Me.TextBox1.Text For Each obj As String In

My.Computer.FileSystem.GetFiles(dirPath, FileIO.SearchOption.SearchAllSubDirectories, "*.bmp")

Me.ComboBox1.Items.Add((obj)) Next End Sub當我們想要取得某個資料夾底下,檔案名稱符合『 *.bmp 』格式的所有檔

案 (包含子資料夾 ) ,則可以用上面的程式碼。

其中參數『 dirPath 』指的是某個資料夾路徑(如 C:\Windows)

Page 22: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetName()EX: CH07-17

'分析路徑,回傳檔名 Dim fileName As String = My.Computer.FileSystem.GetName("c:\

test\book.txt") MsgBox(fileName)

使用 My.Computer.FileSystem.GetName( path)會取得檔案路徑中的檔名部分,上面的範例中,傳回的值會是『 book.txt 』。

Page 23: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.GetParentPath()EX: CH07-18

'分析路徑,回傳檔案路徑(不包含檔名) Dim fileDir As String =

My.Computer.FileSystem.GetParentPath("c:\test\book.txt") MsgBox(fileDir)

使用 My.Computer.FileSystem.GetParentPath( path)會取得去除檔名後的檔案路徑,上面的範例會取得『 c:\test 』。

Page 24: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.MoveFile()EX: CH07-19

'移動檔案,當目的地位置有相同名稱的檔案,直接覆蓋掉 My.Computer.FileSystem.MoveFile("c:\test\book.txt", "d:\files\

testFile\book.txt", True)

'移動檔案,當目的地位置有相同名稱的檔案,出現對話視窗讓使用者選擇是否要覆蓋

My.Computer.FileSystem.MoveFile("c:\test\book.txt", "d:\files\testFile\book.txt", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)

上面的程式碼,會將 c 槽 test 資料夾底下的『 book.txt 』檔案移動到 d 槽的 files 的 testFile 資料夾底下,移動後 c 槽下面的 book.txt 檔案會被刪除。

其中展示了兩種常用的檔案移動方式,一種是設定若當目的地已經存在相同檔案名稱,則直接覆蓋;另一種則會先出現對話視窗,讓使用者選擇是否要覆蓋。

Page 25: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.RenameFile()EX: CH07-20

'更改檔案名稱 My.Computer.FileSystem.RenameFile("c:\test\book.txt", "ch7.txt")

將 c 槽 test 資料夾底下的 book.txt 更名為『 ch7.txt 』。

Page 26: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.ReadAllBytes()EX:

'將檔案以 byte陣列的方式讀入 Dim buf() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\

test\book.txt")

將 c 槽 test 資料夾底下的 book.txt 檔案,用 My.Computer.FileSystem.ReadAllBytes() 讀入,得到一個 byte陣列( buf)。

這種形式的存取方式,多半用在檔案為 Binary形式的內容,例如圖形檔 (.jpg, .gif) 、執行檔 (.exe) 、或是編碼過後的特定格式檔案 (.rar, .doc)… 等。

Page 27: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.ReadAllText()EX:

'將檔案以 Text的方式讀入 Dim buffer As String = My.Computer.FileSystem. ReadAllText ("c:\

test\book.txt")

這段程式碼,是將 c 槽 test 資料夾底下的 book.txt 檔案,用 My.Computer.FileSystem. ReadAllText () 讀入,得到一個 String型別的字串( buffer)

Page 28: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.WriteAllText()EX:

'用My.Computer.FileSystem.WriteAllText儲存檔案My.Computer.FileSystem.WriteAllText(Me.SaveFileDialog1.FileName,

Me.TextBox1.Text, False, System.Text.Encoding.Default)

這一段程式碼功能是將文字存檔。 ReadAllText與WriteAllText 這兩個函式的預設編碼方式都是 UTF-8 ,所以在這裡我們也必須指定編碼方式。

Page 29: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.FileSystem.WriteAllBytes()EX: CH07-21

Dim words As String = "" For i As Integer = 1 To 5000 words += "這是一份測試文件 " & vbCrLf Next

'以 Binary型態儲存 Dim buf() As Byte =

System.Text.Encoding.Default.GetBytes(words) My.Computer.FileSystem.WriteAllBytes("C:\test\info.txt", buf,

False)

'以文字型態儲存 My.Computer.FileSystem.WriteAllText("C:\test\info1.txt", words,

False)

將 5000 行『這是一份測試文件』這幾個字寫到 c 槽 test 資料夾底下的 info.txt 檔案中。上面這段程式碼,上半部是將『測試文件』這幾個字先轉成 Binary形式,也就是 Byte 格式的陣列,然後再儲存到檔案 info.txt 當中。

下半部則是將『測試文件』這幾個字直接寫入 C:\info1.txt 檔案當中,您可以比較兩者實際執行後的結果,會發現在檔案大小上有著明顯的差異。

Page 30: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

DemoDemo 小作家

Page 31: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

列印功能列印功能

Page 32: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念.NET Framework 2.0 中的列印控制項

用來在 Windows 架構應用程式中設定列印的頁面詳細資料。 用來選取印表機、選擇列印頁面以

及決定其他與列印相關的設定。

PrintDocument 實際負責列印功能,可用來設定列印內容及列印文件能力的屬性。它可以與 PrintDialog 元件一起用於控制與文件列印相關的所有事項。

PrintPreviewControl 是用來顯示在列印 PrintDocument 時會出現的外觀。

PrintPreviewDialog 控制項是預先設定的對話方塊,用來顯示文件列印後的樣子。

Page 33: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PageSetupDialog

基本功能:設定列印紙張格式。

使用時機:需要調整列印紙張的規格。

外觀:當 PageSetupDialog 被拖曳出來後,會出現在 component tray 。

PageSetupDialog 可用來調整列印時紙張邊界的設定、紙張直印或橫印…等功能。

Page 34: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PageSetupDialog

當我們使用 PrintDocument 這個控制項 (物件 )作為 PageSetupDialog初始化的參數時, PrintDocument 元件對於頁面的參數設定會隨著 PageSetupDialog 的設定而更動,所以我們無須另外撰寫程式碼設定更改後的數據:

0083: ' 列印設定 ToolStripButton_Click0084: Private Sub 列印設定 ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 列印設定 ToolStripButton.Click0085: '給定要設定列印格式的文件0086: Me.PageSetupDialog1.Document = Me.PrintDocument10087 : '開啟列印設定對話視窗0088: Me.PageSetupDialog1.ShowDialog()0089: End Sub 我們可以發現,在 Visual Basic 2005 中的列

印動作,是透過『 PrintDocument 』來進行,而我們則可以透過『 PageSetupDialog 』控制項,來設定『 PrintDocument 』的列印參數與狀態。

Page 35: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PrintDialog

基本功能:列印的對話視窗。

使用時機:在列印之前,設定列印幾份、列印範圍等。

外觀:當 PrintDialog 被拖曳出來後,會出現在 component tray 。

Page 36: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PrintDialog

PrintDialog 是列印對話視窗,與一般我們按下印表機後出現的對話視窗相同,可以調整列印範圍、選擇印表機…等。

使用 PrintDialog1.ShowDialog() 之前,我們一樣要先將 PrintDocument 指定給 PrintDialog ,作為 PrintDialog 的初始化版面設定:

0101 : '列印 ToolStripButton_Click

0102 : Private Sub 列印 ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 列印 ToolStripButton.Click

0103 : ' 設定 doc ,取得要列印的文字0104 : doc = Me.TextBox1.Text

0105 : ' 給定要列印的文件格式0106 : Me.PrintDialog1.Document = Me.PrintDocument1

0107 :0108 : '按下確定後開始列印0109 : If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

0110 : PrintDocument1.Print()

0111 : End If

0112 : End Sub

『 PrintDialog1 』設定要列印文件為『 PrintDocument1 』

呼叫 PrintDocument1的『 Print方法』實際把文件列印出來。

Page 37: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PrintDocument

基本功能:描述列印版面設置的物件。

使用時機: PrintDocument 會動態的跟著 PageSetupDialog 的設定更改屬性值,也可以直接指定給 PrintPreviewControl 或 PrintDialog ,當作列印版面初始化的依據。您可以透過 PrintDocument 控制項來實際列印一份文件。

請注意,列印動作可透過『 PrintDocument1.Print() 』方法來完成,而要列印的內容,則是透過『 PrintDocument1_PrintPage 』事件當中的參數『 e 』來指定,並且以繪製的方式輸出到印表機上。

Page 38: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PrintDocument

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

'設定字型 Dim myFont As New Font(" 新明細體 ", 30, FontStyle.Regular)

'確保文字不會被腰斬,我們預計最後一行不印文字,所以減去myFont.GetHeight(e.Graphics) Dim strSize As New Size(e.MarginBounds.Width, e.MarginBounds.Height - myFont.GetHeight(e.Graphics))

'確保文字 (英文單字 ) 不會被分成兩行 Dim strFormat As New StringFormat

strFormat.Trimming = StringTrimming.Word

e.Graphics.DrawString("test123", myFont, Brushes.Black, e.MarginBounds, strFormat)

End Sub

請注意,列印動作可透過『 PrintDocument1.Print() 』方法來完成,而要列印的內容,則是透過『 PrintDocument1_PrintPage 』事件當中的參數『 e 』來指定,並且以繪製的方式輸出到印表機上。

Page 39: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PrintPreviewDialog

基本功能:預覽列印的對話視窗。

使用時機:提供使用者預覽的功能,查看文件列印後的狀況,排版適不適合。

外觀:當 PrintPreviewDialog 被拖曳出來後,會出現在 component tray 。

Page 40: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念PrintPreviewDialog

0091: ' 預覽列印 ToolStripButton_Click0092: Private Sub 預覽列印 ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 預覽列印 ToolStripButton.Click0093: '設定 doc ,取得要列印的文字0094: doc = Me.TextBox1.Text0095: '給定要預覽的文件格式0096: Me.PrintPreviewDialog1.Document = Me.PrintDocument10097 : '開啟預覽列印對話視窗0098: Me.PrintPreviewDialog1.ShowDialog()0099: End Sub

Page 41: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

網路功能網路功能

Page 42: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.Network

My.Computer.Network.DownloadFile("http://www.google.com.tw/images/hp0.gif", "C:\123.gif")

上面這行程式可以將Google網站的 Logo圖檔下載至硬碟中,並儲存到 C:\123.gif

Page 43: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.Network

My.Computer.Network.DownloadFile(URL, "C:\1234.exe", "", "", True, 30, True, FileIO.UICancelOption.DoNothing)

此外,您也可以透過上面的指令碼,讓下載時顯示進度:

Page 44: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念My.Computer.Network

您可以透過底下的程式碼,將檔案上傳至 ftp :Dim FileToUpload As String = "C:\1234.exe"Dim address As String = "ftp://127.0.0.1:8093/emag_files/123.exe"Dim UserName As String = ""Dim Password As String = ""My.Computer.Network.UploadFile(FileToUpload, address, UserName, Password, True, 30, FileIO.UICancelOption.DoNothing)

請注意其中的 address參數,該參數要包含完整的路徑,如果 FTP 使用的是特定的 port ,則必須一併寫入,若該 FTP 有帳號密碼,也需要一併傳入。執行的結果如下:

Page 45: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念透過WebRequest與WebResponse取得網頁內容

WebRequest與WebResponse 的命名空間在『 System.Net 』底下,所以在使用 WebRequest或WebResponse 之前,要先在程式碼的最前面加上『 Imports System.Net 』,如果沒有引用,也可以用『 System.Net. WebRequest 』或『 System.Net. WebResponse 』。

簡單的說,我們可以透過WebRequest. Create方法,與遠端的網頁取得聯繫,然後再用WebRequest. GetResponse取得網頁的回應,並且用WebResponse接收。

我們在程式上方寫『 Imports System.Net 』所以這裡才可以直接宣告。

'取得與網頁的聯繫Dim webReq As WebRequest = WebRequest.Create(“網址” )Dim webRes As WebResponse = webReq.GetResponse

Page 46: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念透過WebRequest與WebResponse取得網頁內容接著用WebResponse. GetResponseStream讀取網際網路上的資料流,然後利用 StreamReader的 ReadToEnd方法,將資料流解讀成可用閱讀的文字:

'取得網頁原始檔Dim st As IO.Stream = webRes.GetResponseStreamDim reader As New IO.StreamReader(st, System.Text.Encoding.Default)Dim html As String = reader.ReadToEnd

Page 47: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念透過WebRequest與WebResponse取得網頁內容

例如,底下的程式碼將會顯示 Goole首頁的 HTML 內容:

'取得網頁內容 Dim webReq As WebRequest = WebRequest.Create("http://www

.google.com.tw") Dim webRes As WebResponse = webReq.GetResponse '取得網頁 Stream Dim st As IO.Stream = webRes.GetResponseStream '透過 StreamReader 讀取網頁 Dim reader As New IO.StreamReader(st, System.Text.Encoding.

Default) '取得網頁內容 Dim html As String = reader.ReadToEnd ' 顯示 MsgBox(html)

Page 48: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念利用 HtmlDocument 類別分析 HTML碼

HtmlDocument 是一個新的類別,通常都搭配WebBrowser控制項使用,用來存取 WebBrowser 上的網頁原始碼。比較特別的是, HtmlDocument 的命名空間位在『 System.Windows.Forms 』底下,與一般連接網路的類別存在『 System.Net 』不同。

例如底下的例子,可以透過 WebBrowser控制項取得網頁內容,並且透過 HtmlDocument 類別來處理網頁內容:

取得網頁資料。

0220 : '取得網頁原始碼0221 : Dim doc As HtmlDocument = Me.WebBrowser1.Document

Page 49: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念利用 mshtml.IHTMLDocument2 類別分析 HTML碼

『 mshtml.IHTMLDocument2 』在用法上與 HtmlDocument 沒有太大的差別,HtmlDocument 是透過 WebBrowser. Document 設定初始值,而 mshtml.IHTMLDocument2 除了可以用 WebBrowser. Document 設定初始值外,也可以用 New 的方式建立。

0038 : '分析網頁原始檔0039 : '記得要 Add Reference →Microsoft HTML Object Library 0040 : Dim doc As mshtml.IHTMLDocument20041 : doc = New mshtml.HTMLDocumentClass0042 : doc.write(Me.RichTextBox1.Text)0047 : For Each img As mshtml.HTMLImg In doc.images… 在中間的程式碼當中,可以透過 img 取得 HTML <img> …標記資訊0052 : Next

Page 50: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念利用 WebClient類別存取網際網路資源

WebClient與WebRequest、WebResponse的命名空間相同,位在『 System.Net』底下。

WebClient 的常用方法:

DownloadFile(下載網路資源 )用法:Public Sub DownloadFile ( _

address As String, _fileName As String _

)

參數名稱 意義 備註address 要下載的網路資源網址。

fileName 下載後的檔案位置。

Page 51: CH07. 檔案存取 Windows 程式設計基本概念 Visual Basic 2005 當中存取檔案的方式 透過 System.IO.File ( 傳統方法 ) 透過 My.Computer.FileSystem (VB2005 新功能

WindowsWindows 程式設計基本概念程式設計基本概念利用 WebClient類別存取網際網路資源

下面的程式碼經由 mshtml.IHTMLDocument2 取得圖片網址列集合後,我們採用跑『 For Each 』迴圈的方式,逐一讀取圖片網址列,並使用 WebClient. DownloadFile 下載圖片: 使用 WebClient類別

0044: '下載圖片0045: Dim load As New WebClient0046:0047 : For Each img As mshtml.HTMLImg In doc.images0048: ' 檢查圖片網址是否正確0049: If img.href.EndsWith("gif") Or img.href.EndsWith("jpg") Then0050 : load.DownloadFile(img.href, Me.Tex_檔案位置 .Text & "\" & My.Computer.FileSystem.GetName(img.href))0051: End If0052: Next

下載圖片。