Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
Try
OFD.Filter = "Binary files (*.bin)|*.bin"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
fullFile = File.ReadAllBytes(OFD.FileName)
Dim writer As New BinaryWriter(OFD.OpenFile)
writer.Seek(&H5B0, SeekOrigin.Begin)
writer.Write(CUShort(&HB1AA))
writer.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
它应该打开文件然后将字节更改为它。但是在打开文件的时候我收到了消息框:
错误无法在流中写入
文件仍未更改。修复我的代码。
答案 0 :(得分:1)
关于OpenFileDialog.OpenFile()辅助方法的MSDN article:
使用只读权限打开用户选择的文件。
相关短语加粗,表示您无法写入文件。您需要使用FileStream。像这样:
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Using fs As New FileStream(OFD.FileName, FileMode.Open, FileAccess.Write, FileShare.None)
Using writer As New BinaryWriter(fs)
writer.Seek(&H5B0, SeekOrigin.Begin)
writer.Write(CUShort(&HB1AA))
End Using
End Using
End If