VB.Net Tutorial/Stream File/File Utilities — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Содержание
Copy a file
Imports System.IO
Imports System.Text
Public Class Tester
Public Shared Sub Main
Dim mySourceFileStream As FileStream
Dim myDestFileStream As FileStream
Dim bteRead() As Byte
Dim intByte As Integer
Dim bteMessage(128) As Byte
Dim strCopyMessage As String = ""
Try
myDestFileStream = New FileStream("dest.txt", FileMode.OpenOrCreate, FileAccess.Write)
bteMessage = Encoding.ASCII.GetBytes(strCopyMessage)
myDestFileStream.Write(bteMessage, 0, bteMessage.Length)
mySourceFileStream = New FileStream("source.txt", FileMode.OpenOrCreate, FileAccess.Read)
intByte = mySourceFileStream.Length
ReDim bteRead(intByte)
mySourceFileStream.Read(bteRead, 0, intByte)
myDestFileStream.Write(bteRead, 0, intByte)
myDestFileStream.Close()
mySourceFileStream.Close()
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
Delete a file
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim f As File
f.Delete("test.txt")
End Sub
End Class
Delete a file with kill
Public Class Tester
Public Shared Sub Main
Kill("Test.csv")
End Sub
End Class
File copy
Imports System.Windows.Forms
public class FileOpenDialogFilter
public Shared Sub Main
System.IO.File.Copy("c:\\test.txt", "c:\\test1.txt", True)
End Sub
End class
Move a File
Public Class Tester
Declare Auto Function MoveFile Lib "KERNEL32.dll" Alias "MoveFile" (ByVal src As String, ByVal
dst As String) As Boolean
Public Shared Sub Main
MoveFile("d:\\test.txt", "c:\\test.txt")
End Sub
End Class
Move a file with File.Move
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim f As File
f.Move("test.txt", "text2.txt")
End Sub
End Class
Unhandled Exception: System.IO.IOException: Cannot create a file when that file already exists. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.WinIOError() at System.IO.File.Move(String sourceFileName, String destFileName) at Tester.Main()
Use FreeFile
Option Strict On
Public Module Checking
Private Const filename As String = "CheckFile.dat"
Public Sub Main()
Dim fn As Integer = FreeFile
Dim fileOpened As Boolean
Try
FileOpen(fn, filename, OpenMode.Append)
fileOpened = True
WriteLine(fn, 1605, Date.Now, "Books", 2.19)
Catch e As Exception
Console.WriteLine(e.GetType().Name & ": " & e.Message)
Finally
If fileOpened Then FileClose(fn)
End Try
End Sub
End Module
Use FreeFile to read a text file
public class Test
public Shared Sub Main
Dim file_num As Integer = FreeFile()
Dim file_name As String = "test.txt"
FileOpen(file_num, file_name, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Do While Not EOF(file_num)
Dim txt As String = LineInput(file_num)
Console.WriteLine(txt)
Loop
FileClose(file_num)
End Sub
End class
Use the HMACSHA1 hashing function to generate a checksum for a file.
Imports System.Text
Imports System.Security.Cryptography
Public Class Tester
Public Shared Sub Main
Dim checksum1 As Byte()
checksum1 = GenerateFileChecksum("test.txt")
End Sub
Public Shared Function GenerateFileChecksum(ByVal filePath As String) As Byte()
Dim hashingFunction As HMACSHA1
Dim hasingBase() As Byte
Dim hashValue() As Byte
Dim inStream As IO.Stream
If (My.ruputer.FileSystem.FileExists(filePath)= False) Then
Throw New IO.FileNotFoundException
Return Nothing
End If
hasingBase = (New UnicodeEncoding).GetBytes("Cookbook")
hashingFunction = New HMACSHA1(hasingBase, True)
inStream = New IO.FileStream(filePath,IO.FileMode.Open, IO.FileAccess.Read)
hashValue = hashingFunction.ruputeHash(inStream)
inStream.Close()
Return hashValue
End Function
End Class