Visual C# 2005程式設計

Similar documents
Visual Basic D 3D

投影片 1

Microsoft PowerPoint - VB14.ppt

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

TC35短信发送程序设计

Microsoft Word - 小心翼翼的二十一點N.doc

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

White Sketchpad PowerPoint Presentation

ThreeDtunnel.doc

untitled

投影片 1

untitled

穨文件1

VB控件教程大全

Chapter 16 集合

untitled

IsPostBack 2

epub83-1

多層次傳銷與獎金系統

PowerPoint Presentation

TwinCAT 1. TwinCAT TwinCAT PLC PLC IEC TwinCAT TwinCAT Masc

3 Driver do Microsoft Access (*.mdb) hisdata IFIX 1.4

untitled

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

ActiveX Control

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

PowerPoint Presentation

untitled

MVB-1001.DOC

Excel VBA Excel Visual Basic for Application

untitled

1 1 Excel VBA 說明 ( ) (_) STEP4 Excel 2 STEP5 A1 1 B2 2 C3 3 STEP6 A1 STEP7 > > 1-11

JavaIO.PDF

星星排列 _for loop Protected Sub Page_Load(ByVal sender As Object, ByVal e As Dim h As Integer = 7 'h 為變數 ' Dim i, j As Integer For i = 1 To h

untitled

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

untitled

<4D F736F F D D342DA57CA7DEA447B14D2DA475B57BBB50BADEB27AC3FEB14DA447B8D5C344>

四川省普通高等学校

2010年3月计算机等级考试四级网络工程师笔试

Microsoft Word - CX1000-HMI_程序开发_PLC通讯

Microsoft PowerPoint - 06.ppt

VB程序设计教程

untitled

全国计算机技术与软件专业技术资格(水平)考试

PowerPoint 簡報

<4D F736F F D20AC4FBDBDA4FBB67DA96CAABA2DA743A67EAFC5AAA95FA7B9BD5A5F2E646F63>

ex

untitled

1.5招募说明书(草案)

基于ECO的UML模型驱动的数据库应用开发1.doc

<4D F736F F D B0D3B77EC3FEA7DEC3C0C476C1C9A5BFA6A1B8D5C3442DB57BA6A1B35DAD702DBEC7ACEC2E646F6378>

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

jsj0.nps

穨ac3-4.PDF

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp

投稿類別:電子工程類

游戏厅捕鱼技巧_天天酷跑游戏技巧 2048游戏技巧,游戏厅打鱼技巧_

Microsoft Word - 序.DOC

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

untitled

MATLAB介紹

untitled

(Methods) Client Server Microsoft Winsock Control VB 1 VB Microsoft Winsock Control 6.0 Microsoft Winsock Control 6.0 1(a). 2

Microsoft Word - ch04三校.doc

Microsoft Word - 01.DOC

3.1 num = 3 ch = 'C' 2

Microsoft PowerPoint - 第14章.ppt

untitled

Microsoft Word - 應用程式設計.doc

untitled

ASP.NET 4.0 專題實務 I 17-1 System.IO 命名空間 要使用檔案, 務必在程式上方宣告 System.IO 命名空間 (Namespace) 如果用在 Inline Code( 把程式跟 HTML 寫在同一個.aspx 檔裡面 ), 必須寫成 : Page Langu

2

untitled

EJB-Programming-4-cn.doc

untitled

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

untitled

第一章 章标题-F2 上空24,下空24

untitled

( Version 0.4 ) 1


科学计算的语言-FORTRAN95

untitled

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

epub 94-3

chp6.ppt

Swing-02.pdf

「人名權威檔」資料庫欄位建置表

第一章

EJB-Programming-3.PDF

C H A P T E R 7 Windows Vista Windows Vista Windows Vista FAT16 FAT32 NTFS NTFS New Technology File System NTFS

Microsoft PowerPoint - OPVB1基本VB.ppt

新版 明解C++入門編

Java

Chapter 9: Objects and Classes

Microsoft PowerPoint - 13_ClassAndObj.ppt

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Transcription:

Visual Basic 2005 程式設計 第 13 章檔案存取

13-1 存取檔案 資料夾與磁碟的相關資訊 13-1-1 存取檔案的相關資訊使用 My.Computer.FileSystem 物件的 GetFileInfo(file) 方法, 傳回值是一個 FileInfo 物件, 比較重要的屬性如下 : CreationTime: 取得或設定檔案建立時間傳回 date Directory: 取得檔案的父資料夾傳回 DirectoryInfo 物件 DirectoryName: 取得檔案父資料夾名稱傳回字串 Exists: 判斷檔案是否存在傳回布林值 Extension: 取得副檔名傳回字串 FullName: 取得檔案完整路徑傳回字串 IsReadOnly: 是否唯讀 LastAccessTime: 取得或設定上次存取時間 LastWriteTime: 取得上次寫入時間 Length : 取得檔案大小 ( 傳回 Long)

\MyProj13-1\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim getinfo As System.IO.FileInfo getinfo = My.Computer.FileSystem.GetFileInfo("C:\test.txt") MsgBox(" 這個檔案的建立時間為 " & getinfo.creationtime & Chr(10) & _ " 這個檔案的副檔名為 " & getinfo.extension & Chr(10) & _ " 這個檔案的大小為 " & getinfo.length & " 位元組 ") Close() End Sub End Class

13-1-2 存取資料夾的相關資訊 使用 My.Computer.FileSystem 物件的 GetDirectoryInfo(dir) 方法, 傳回值是一個 DirectoryInfo 物件, 比較重要的屬性如下 : CreationTime Exists Extension FullName LastAccessTime LastWriteTime Name Parent Root Attributes

\MyProj13-2\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim getinfo As System.IO.DirectoryInfo getinfo = My.Computer.FileSystem.GetDirectoryInfo("C:\WINDOWS") If (getinfo.attributes And System.IO.FileAttributes.ReadOnly) > 0 Then MsgBox("C:\WINDOWS 是唯讀的 ") Else MsgBox("C:\WINDOWS 不是唯讀的 ") End If Close() End Sub End Class

13-1-3 存取磁碟的相關資訊 使用 My.Computer.FileSystem 物件的 GetDriveInfo(drive) 方法, 傳回值是一個 DriveInfo 物件, 比較重要的屬性如下 : Available FreeSpace DriveFormat DriveType RootDirectory TotalFreeSpace TotalSize Volume Label

\MyProj13-3\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim getinfo As System.IO.DriveInfo getinfo = My.Computer.FileSystem.GetDriveInfo("C:\") MsgBox("C: 磁碟的標籤為 " & getinfo.volumelabel & Chr(10) & _ "C: 磁碟的類型為 " & getinfo.drivetype & Chr(10) & _ "C: 磁碟的全部空間為 " & getinfo.totalsize & Chr(10) & _ "C: 磁碟的檔案系統為 " & getinfo.driveformat) Close() End Sub End Class

13-2 建立 刪除 搬移 複製檔案與資料夾 13-2-1 取得資料夾內的子資料夾集合 使用 My.Computer.FileSystem 物件的 GetDirectories(dir) 方法 For Each DirName As String In My.Computer.FileSystem.GetDirectories("C:\ Inetpub") Console.WriteLine(DirName) Next

13-2-2 取得資料夾內的檔案集合 使用 My.Computer.FileSystem 物件的 GetFiles(dir) 方法 For Each file As String In My.Computer.FileSystem.GetFiles("C:\Inetpub\www root") Console.WriteLine(file) Next

13-2-3 建立檔案 使用 System.IO.File 類別的 Create(file) 方法 Dim file As System.IO.FileStream = System.IO.File.Create("C:\test.txt") Create(file As String, buffersize As Integer) Create(file As String, buffersize As Integer, options As FileOptions) Create(file As String, buffersize As Integer, options As FileOptions, fs As FileSecurity)

13-2-4 刪除檔案 DeleteFile(file As String) DeleteFile(file As String, showui As UIOption, recycle As RecycleOption) DeleteFile(file As String, showui As UIOption, recycle As RecycleOption, onusercancel As UICancelOption) My.Computer.FileSystem.DeleteFile("C:\test.txt") My.Computer.FileSystem.DeleteFile("C:\test.txt", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)

13-2-5 搬移檔案 MoveFile(source As String, destination As String) MoveFile(source As String, destination As String, overwrite As Boolean) MoveFile(source As String, destination As String, showui As UIOption) MoveFile(source As String, destination As String, showui As UIOption, As UICancelOption) My.Computer.FileSystem.MoveFile("C:\Dir1\test.txt ", "C:\Dir2\test.txt") My.Computer.FileSystem.MoveFile("C:\Dir1\test.txt ", "C:\Dir2\test2.txt")

13-2-6 複製檔案 CopyFile(source As String, destination As String) CopyFile(source As String, destination As String, overwrite As Boolean) CopyFile(source As String, destination As String, showui As UIOption) CopyFile(source As String, destination As String, showui As UIOption, As UICancelOption) My.Computer.FileSystem.CopyFile("C:\Dir1\test.txt ", "C:\Dir1\test2.txt") My.Computer.FileSystem.CopyFile("C:\Dir1\test.txt ", "C:\Dir2\test2.txt", True)

13-2-9 建立資料夾 使用 My.Computer.FileSystem 物件的 CreateDirectory(dir) 方法 My.Computer.FileSystem.CreateDirectory("C:\Dir 13-2-7 重新命名檔案 使用 My.Computer.FileSystem 物件的 RenameFile(file, newname) 方法 My.Computer.FileSystem.RenameFile("C:\test.txt ", "test2.txt") 13-2-8 重新命名資料夾 使用 My.Computer.FileSystem 物件的 RenameDirectory(dir, newname) 方法 My.Computer.FileSystem. RenameDirectory("C:\Dir1", "Dir2")

13-2-10 10 刪除資料夾 DeleteDirectory(dir As String, ondirectorynotempty As DeleteDirectoryOption) DeleteDirectory(dir As String, showui As UIOption, recycle As RecycleOption) DeleteDirectory(dir As String, showui As UIOption, recycle As RecycleOption, onusercancel As UICancelOption) My.Computer.FileSystem.DeleteDirectory("C:\Dir1", _ FileIO.DeleteDirectoryOption.DeleteAllContents) My.Computer.FileSystem.DeleteDirectory("C:\Dir1", _ FileIO.DeleteDirectoryOption.ThrowIfDirectoryNon

13-2-11 11 搬移資料夾 MoveDirectory(source As String, destination As String) MoveDirectory(source As String, destination As String, overwrite As Boolean) MoveDirectory(source As String, destination As String, showui As UIOption) MoveDirectory(source As String, destination As String, showui As UIOption, As UICancelOption) My.Computer.FileSystem. MoveDirectory("C:\Dir1", "C:\Dir2") My.Computer.FileSystem. MoveDirectory("C:\Dir1", "C:\Dir2", True)

13-2-12 12 複製資料夾 CopyDirectory(source As String, destination As String) CopyDirectory(source As String, destination As String, overwrite As Boolean) CopyDirectory(source As String, destination As String, showui As UIOption) CopyDirectory(source As String, destination As String, showui As UIOption, As UICancelOption) My.Computer.FileSystem.CopyFile("C:\Dir1", "C:\Dir2", True)

13-3-1 讀取文字檔 13-3 讀取檔案 \MyProj13-4\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim content As String = My.Computer.FileSystem.ReadAllText("C:\poem1.txt", _ System.Text.Encoding.Unicode) MsgBox(content) Close() End Sub End Class

\MyProj13-5\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim textreader As System.IO.StreamReader = _ My.Computer.FileSystem.OpenTextFileReader("C:\poem1.txt", _ System.Text.Encoding.Unicode) Dim content As String = "" Dim aline As String = textreader.readline() While Not (aline Is Nothing) content = content & aline & Chr(10) aline = textreader.readline() End While textreader.close() MsgBox(content) Close() End Sub End Class

13-3-2 讀取以符號分隔的文字檔 我們可以透過 TextFieldParser 物件讀取以符號分隔的文字檔 下面是一個以逗號分隔的文字檔 ( 編碼方式為 Unicode), 裡面有四個文字行, 每行各有兩個欄位 C:\poem2.txt 鳳凰臺上鳳凰遊, 鳳去臺空江自流吳宮花草埋幽徑, 晉代衣冠成古邱三山半落青又外, 二水中分白鷺洲總為浮雲能蔽日, 長安不見使人愁

\MyProj13-6\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Using textparser As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\poem2.txt") textparser.textfieldtype = FileIO.FieldType.Delimited textparser.setdelimiters(",") Dim content As String = "" While Not textparser.endofdata Try Dim row As String() = textparser.readfields() For Each field As String In row content = content & field & Chr(10) Next Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException MsgBox(" 無法使用指定的格式剖析文字檔 ") End Try End While MsgBox(content) End Using Close() End Sub End Class

13-3-3 讀取具有固定寬度的文字檔 我們可以透過 TextFieldParser 物件讀取具有固定寬度的文字檔 下面的文字檔有兩個文字行, 每行各有四個欄位, 寬度分別為 2 3-1 個字元 (-1 表示可變動 ) C:\sample.txt 123456789 ABCDEFGHIJKLM

\MyProj13-7\Form1.vb Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Using textparser As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\sample.txt") textparser.textfieldtype = FileIO.FieldType.FixedWidth textparser.setfieldwidths(2, 3, -1) Dim content As String = "" While Not textparser.endofdata Try Dim row As String() = textparser.readfields() For Each field As String In row content = content & field & Chr(10) Next Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException MsgBox(" 無法使用指定的格式剖析文字檔 ") End Try End While MsgBox(content) End Using Close() End Sub End Class

13-3-4 讀取二進位檔 我們可以使用 My.Computer.FileSystem 物件的 ReadAllBytes(file) 方法讀取二進位檔的所有內容 Dim content As Byte() = My.Computer.FileSystem.ReadAllBytes("C:/picture. jpg")

13-4 寫入檔案 13-4-1 寫入文字檔 我們可以使用 My.Computer.FileSystem 物件的 WriteAllText(file, text, append) 或 WriteAllText(file, text, append, encoding) 方法將文字寫入文字檔 Dim streamwriter As System.IO.StreamWriter = _ My.Computer.FileSystem.OpenTextFileWriter("C:\te st.txt", True) streamwriter.writeline(" 白日依山盡 ") streamwriter.writeline(" 黃河入海流 ")

13-4-2 寫入二進位檔 我們可以使用 My.Computer.FileSystem 物件的 WriteAllBytes(file, data, append) 方法將資料寫入文字檔 My.Computer.FileSystem.WriteAllBytes("C:/picture.j pg", mydata, True)