VB.Net Tutorial/Stream File/FileSystem — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Содержание
- 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
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
35,532,111,872 bytes 35,532,111,872 bytes 60,003,381,248 bytes
Find in files
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
"Class" found in: C:\Java_Dev\WEB\dev\VB\Addaprinterprocess.vb C:\Java_Dev\WEB\dev\VB\Addfirstpointandsizetoanotherpoint.vb
List all available Drives
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
C:\ D:\
My.Computer.FileSystem.CurrentDirectory
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
My.Computer.FileSystem.DirectoryExists
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
The directory already exists.
My.Computer.FileSystem.FileExists(""FileName"")
Public Class Tester
Public Shared Sub Main
Console.WriteLine(My.ruputer.FileSystem.FileExists("test.txt"))
End Sub
End Class
True
My.Computer.FileSystem.FindInFiles
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
My.Computer.FileSystem.GetParentPath
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
C:\Java_Dev\WEB\dev
My.Computer.FileSystem.ReadAllText
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
Column 1, Column 2, Column 3 1.23, 4.56, 7.89 3.21, 6.54, 9.87
My.Computer.FileSystem.WriteAllText
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
Test for a valid directory
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
The directory already exists.
Use pattern(wildcard) to match directories
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
Use pattern(wildcard) to match files
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
DebugMessages.txt test.txt test1.txt