VB.Net - Windows文件系統(tǒng)

2018-12-19 09:30 更新
VB.Net允許您使用各種目錄和文件相關的類,如DirectoryInfo類和FileInfo類處理目錄和文件。

DirectoryInfo類

DirectoryInfo類是從FileSystemInfo類派生的。 它有各種方法來創(chuàng)建,移動和瀏覽目錄和子目錄。 此類不能繼承。

以下是DirectoryInfo類的一些常用屬性:

S.N屬性名稱和說明
1

Attributes

Gets the attributes for the current file or directory.

獲取當前文件或目錄的屬性。

2

CreationTime

Gets the creation time of the current file or directory.

獲取當前文件或目錄的創(chuàng)建時間。

3

Exists

Gets a Boolean value indicating whether the directory exists.

獲取指示該目錄是否存在的布爾值。

4

Extension

Gets the string representing the file extension.

獲取表示文件擴展名的字符串。

5

FullName

Gets the full path of the directory or file.

獲取目錄或文件的完整路徑。

6

LastAccessTime

Gets the time the current file or directory was last accessed.

獲取上次訪問當前文件或目錄的時間。

7

Name

Gets the name of this DirectoryInfo instance.

獲取此DirectoryInfo實例的名稱。


以下是DirectoryInfo類的一些常用方法:

S.N方法名稱和用途
1

Public Sub Create

Creates a directory.

創(chuàng)建目錄。

2

Public Function CreateSubdirectory (path As String ) As DirectoryInfo

Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

在指定的路徑上創(chuàng)建子目錄或子目錄。 指定的路徑可以相對于DirectoryInfo類的此實例。

3

Public Overrides Sub Delete

Deletes this DirectoryInfo if it is empty.

如果DirectoryInfo為空,則刪除它。

4

Public Function GetDirectories As DirectoryInfo()

Returns the subdirectories of the current directory.

返回當前目錄的子目錄。

5

Public Function GetFiles As FileInfo()

Returns a file list from the current directory.

從當前目錄返回文件列表。

有關屬性和方法的完整列表,請訪問Microsoft的文檔。


FileInfo類

FileInfo類是從FileSystemInfo類派生的。 它具有用于創(chuàng)建,復制,刪除,移動和打開文件的屬性和實例方法,并且有助于創(chuàng)建FileStream對象。 此類不能繼承。

以下是FileInfo類的一些常用屬性:

S.N屬性名稱和說明
1

Attributes

Gets the attributes for the current file.

獲取當前文件的屬性。

2

CreationTime

Gets the creation time of the current file.

獲取當前文件的創(chuàng)建時間。

3

Directory

Gets an instance of the directory, which the file belongs to.

獲取該文件所屬的目錄的實例。

4

Exists

Gets a Boolean value indicating whether the file exists.

獲取指示文件是否存在的布爾值。

5

Extension

Gets the string representing the file extension.

獲取表示文件擴展名的字符串。

6

FullName

Gets the full path of the file.

獲取文件的完整路徑。

7

LastAccessTime

Gets the time the current file was last accessed.

獲取當前文件上次訪問的時間。

8

LastWriteTime

Gets the time of the last written activity of the file.

獲取文件的上次寫入活動的時間。

9

Length

Gets the size, in bytes, of the current file.

獲取當前文件的大?。ㄒ宰止?jié)為單位)。

10

Name

Gets the name of the file.

獲取文件的名稱。


以下是FileInfo類的一些常用方法:

S.N方法名稱和用途
1

Public Function AppendText As StreamWriter

Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

創(chuàng)建一個StreamWriter,將文本附加到由FileInfo的實例表示的文件。

2

Public Function Create As FileStream

Creates a file.

創(chuàng)建文件。

3

Public Overrides Sub Delete

Deletes a file permanently.

永久刪除文件。

4

Public Sub MoveTo (destFileName As String )

Moves a specified file to a new location, providing the option to specify a new file name.

將指定的文件移動到新位置,提供指定新文件名的選項。

5

Public Function Open (mode As FileMode) As FileStream

Opens a file in the specified mode.

以指定模式打開文件。

6

Public Function Open (mode As FileMode, access As FileAccess ) As FileStream

Opens a file in the specified mode with read, write, or read/write access.

以指定模式打開具有讀取,寫入或讀/寫訪問權限的文件。

7

Public Function Open (mode As FileMode, access As FileAccess, share As FileShare ) As FileStream

Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.

以指定模式打開具有讀取,寫入或讀/寫訪問權限以及指定的共享選項的文件。

8

Public Function OpenRead As FileStream

Creates a read-only FileStream

創(chuàng)建只讀FileStream

9

Public Function OpenWrite As FileStream

Creates a write-only FileStream.

創(chuàng)建只寫FileStream。

有關屬性和方法的完整列表,請訪問Microsoft的文檔

示例:

以下示例演示了上述類的使用:

Imports System.IO
Module fileProg
   Sub Main()
      'creating a DirectoryInfo object
      Dim mydir As DirectoryInfo = New DirectoryInfo("c:\Windows")
      ' getting the files in the directory, their names and size
      Dim f As FileInfo() = mydir.GetFiles()
      Dim file As FileInfo
      For Each file In f
          Console.WriteLine("File Name: {0} Size: {1}  ", file.Name, file.Length)
      Next file
      Console.ReadKey()
   End Sub
End Module
當編譯和運行程序時,它在Windows目錄中顯示文件名及其大小。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號