文件I / O帮助而不是FileOpen

时间:2011-12-29 20:34:06

标签: .net vb.net

所以我一直在我的.net应用程序中使用FileOpen,FileGet和FileClose。但是,有一位朋友告诉我,我应该使用IO作为更快的速度,并且整体上更好。我一直在浏览文档,我仍然有点困惑。这是我的文件绑定器的原始代码:

'Opens file1 by user choice and gets contents
FileOpen(1, TextBox1.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Default)
filein = Space(LOF(1))
FileGet(1, filein)
FileClose(1)

'Opens file2 and gets contents
FileOpen(1, Application.StartupPath & "\file2.exe", OpenMode.Binary, OpenAccess.Read, OpenShare.Default)
file2 = Space(LOF(1))
FileGet(1, file2)
FileClose(1)

'Opens final file that will contain both binded files
FileOpen(1, filename, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Default)
FilePut(1, file2 & filesplit & filein)
FileClose(1)

我试过使用IO.File.WriteAllBytes和IO.File.WriteAllText没有运气。我如何改变我的代码来使用IO?感谢

1 个答案:

答案 0 :(得分:0)

以下是可用于将文件读入字节数组的代码:

Dim aBytes As Byte() = Nothing

Using oStream As New FileStream(sFileName, FileMode.Open, FileAccess.Read)
    If oStream IsNot Nothing Then
        Dim wLen As Integer

        wLen = CInt(New FileInfo(PathName).Length)
        ReDim aBytes(wLen - 1)
        oStream.Read(aBytes, 0, wLen)
        oStream.Close()
    End If
End Using

将字节数组写入文件:

File.WriteAllBytes(sFileName, aBytes)