Savefile 对话框保存空白文件
Savefile dialog saving blank file
有人可以帮我修复这段代码吗?问题是它保存空白文件。
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
它应该保存修改后的二进制文件:
但它保存空白文件。没有任何偏移
调用 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
有人可以帮我修复这段代码吗?问题是它保存空白文件。
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
它应该保存修改后的二进制文件: 但它保存空白文件。没有任何偏移
调用 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