有人可以帮我解决这个问题吗?问题是它保存空白文件。
Dim OFD As New OpenFileDialog
Try
OFD.Filter = "Binary files (*.bin)|*.bin"
Finally
End Try
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim b() As Byte = System.IO.File.ReadAllBytes(OFD.FileName)
If (b.Count And 1) = 1 Then
MessageBox.Show("File is not an even number of bytes, so is not filled with 16-bit values")
Else
For i As Integer = 0 To b.Count - 2 Step 2
b(i) = b(i) Xor b(i + 1) 'these three lines efficiently swap two bytes in place
b(i + 1) = b(i) Xor b(i + 1)
b(i) = b(i) Xor b(i + 1)
Next
**Dim SFD As New SaveFileDialog()
Try
SFD.Filter = "Binary files (*.bin)|*.bin"
Finally
End Try
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then**
End If
End If
End If
End Sub
它应该保存修改后的二进制文件: 但它保存了空白文件。没有任何抵消
答案 0 :(得分:1)
对SaveFileDialog.ShowDialog
的调用不会自动创建该文件。它只是允许用户提供一个文件名,然后您的代码可以使用该文件名来实际保存文件内容。
用户提供文件名后,您需要使用System.IO.File.WriteAllBytes
。您可以从SaveFileDialog.FileName
检索他们提供的名称。这是一个例子:
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
System.IO.File.WriteAllBytes(SFD.FileName, b)
End If