VB.Net Tutorial/Stream File/FileSystem
Содержание
- 1 Drive TotalFreeSpace, AvailableFreeSpace and TotalSize
- 2 Find in files
- 3 List all available Drives
- 4 My.Computer.FileSystem.CurrentDirectory
- 5 My.Computer.FileSystem.DirectoryExists
- 6 My.Computer.FileSystem.FileExists(""FileName"")
- 7 My.Computer.FileSystem.FindInFiles
- 8 My.Computer.FileSystem.GetParentPath
- 9 My.Computer.FileSystem.ReadAllText
- 10 My.Computer.FileSystem.WriteAllText
- 11 Test for a valid directory
- 12 Use pattern(wildcard) to match directories
- 13 Use pattern(wildcard) to match files
Drive TotalFreeSpace, AvailableFreeSpace and TotalSize
<source lang="vbnet">Public Class Tester
Public Shared Sub Main Dim oneDrive As IO.DriveInfo = My.ruputer.FileSystem.GetDriveInfo("C:\\") Console.WriteLine(Format(oneDrive.TotalFreeSpace,"#,##0") & " bytes") Console.WriteLine(Format(oneDrive.AvailableFreeSpace,"#,##0") & " bytes") Console.WriteLine(Format(oneDrive.TotalSize,"#,##0") & " bytes") End Sub
End Class</source>
35,532,111,872 bytes 35,532,111,872 bytes 60,003,381,248 bytes
Find in files
<source lang="vbnet">Option Strict On Imports Microsoft.VisualBasic.FileIO Imports System.Collections.ObjectModel Public Module FileFind
Public Sub Main() Dim filespec() As String = {"*.vb"} Dim matches As ReadOnlyCollection(Of String) matches = My.ruputer.FileSystem.FindInFiles(".", "Class", False,SearchOption.SearchTopLevelO
nly, filespec)
If matches.Count > 0 Then Console.WriteLine(""Class" found in:") For Each filename As String In matches Console.WriteLine(" " & filename) Next Else Console.WriteLine("No matches found.") End If End Sub
End Module</source>
"Class" found in: C:\Java_Dev\WEB\dev\VB\Addaprinterprocess.vb C:\Java_Dev\WEB\dev\VB\Addfirstpointandsizetoanotherpoint.vb
List all available Drives
<source lang="vbnet">Option Strict On Imports System.Collections.ObjectModel Imports System.IO Public Module IterateDrives
Public Sub Main() Dim drives As ReadOnlyCollection(Of DriveInfo) drives = My.ruputer.FileSystem.Drives For Each drive As DriveInfo In drives Console.WriteLine(drive.Name) Next End Sub
End Module</source>
C:\ D:\
My.Computer.FileSystem.CurrentDirectory
<source lang="vbnet">Public Class Tester
Public Shared Sub Main Dim headings() As String = {"Alpha", "Beta", "Gamma"} Dim workText As String = String.Join(",", headings) workText &= "1.1, 2.3, 4.5" & vbNewLine Dim filePath As String = My.ruputer.FileSystem.CurrentDirectory & "\Test.csv" My.ruputer.FileSystem.WriteAllText(filePath, workText, False)
End Sub
End Class</source>
My.Computer.FileSystem.DirectoryExists
<source lang="vbnet">Public Class Tester
Public Shared Sub Main If (My.ruputer.FileSystem.DirectoryExists("C:\\")) Then Console.WriteLine("The directory already exists.") Else Console.WriteLine("The directory does not exist, " & _ "or is part of an invalid path.") End If End Sub
End Class</source>
The directory already exists.
My.Computer.FileSystem.FileExists(""FileName"")
<source lang="vbnet">Public Class Tester
Public Shared Sub Main Console.WriteLine(My.ruputer.FileSystem.FileExists("test.txt")) End Sub
End Class</source>
True
My.Computer.FileSystem.FindInFiles
<source lang="vbnet">Imports System.Collections.ObjectModel Imports System.IO public class Test
public Shared Sub Main Dim file_names As ReadOnlyCollection(Of String) file_names = My.ruputer.FileSystem.FindInFiles("c:\\", "searchFor.txt",True, _ FileIO.SearchOption.SearchAllSubDirectories) For Each file_name As String In file_names Console.WriteLine(file_name) Next file_name End Sub
End class</source>
My.Computer.FileSystem.GetParentPath
<source lang="vbnet">public class Test
public Shared Sub Main Dim start_dir As String = System.Windows.Forms.Application.StartupPath start_dir = My.ruputer.FileSystem.GetParentPath(start_dir) Console.WriteLine(start_dir) End Sub
End class</source>
C:\Java_Dev\WEB\dev
My.Computer.FileSystem.ReadAllText
<source lang="vbnet">Public Class Tester
Public Shared Sub Main Dim csvFile As String = My.Application.Info.DirectoryPath & "\Test.csv" Dim outFile As IO.StreamWriter = My.ruputer.FileSystem.OpenTextFileWriter(csvFile, False)
outFile.WriteLine("Column 1, Column 2, Column 3") outFile.WriteLine("1.23, 4.56, 7.89") outFile.WriteLine("3.21, 6.54, 9.87") outFile.Close() Console.WriteLine(My.ruputer.FileSystem.ReadAllText(csvFile)) End Sub
End Class</source>
Column 1, Column 2, Column 3 1.23, 4.56, 7.89 3.21, 6.54, 9.87
My.Computer.FileSystem.WriteAllText
<source lang="vbnet">Public Class Tester
Public Shared Sub Main Dim headings() As String = {"Alpha", "Beta", "Gamma"} Dim workText As String = String.Join(",", headings) workText &= "1.1, 2.3, 4.5" & vbNewLine Dim filePath As String = My.ruputer.FileSystem.CurrentDirectory & "\Test.csv" My.ruputer.FileSystem.WriteAllText(filePath, workText, False)
End Sub
End Class</source>
Test for a valid directory
<source lang="vbnet">Public Class Tester
Public Shared Sub Main If (My.ruputer.FileSystem.DirectoryExists("C:\\")) Then Console.WriteLine("The directory already exists.") Else Console.WriteLine("The directory does not exist, " & _ "or is part of an invalid path.") End If End Sub
End Class</source>
The directory already exists.
Use pattern(wildcard) to match directories
<source lang="vbnet">Imports System.Runtime.Serialization Public Class Tester
Public Shared Sub Main For Each oneItem As String In My.ruputer.FileSystem.GetDirectories("c:\\",FileIO.SearchOption.SearchTopLevelOnly, "*.txt") Console.WriteLine("[" & My.ruputer.FileSystem.GetName(oneItem) & "]") Next oneItem End Sub
End Class</source>
Use pattern(wildcard) to match files
<source lang="vbnet">Imports System.Runtime.Serialization Public Class Tester
Public Shared Sub Main For Each oneItem As String In My.ruputer.FileSystem.GetFiles("c:\\", _ FileIO.SearchOption.SearchTopLevelOnly,"*.txt") Console.WriteLine(My.ruputer.FileSystem.GetName(oneItem)) Next oneItem End Sub
End Class</source>
DebugMessages.txt test.txt test1.txt