是否可以在 VB 程序中打开自定义文件类型。
例如:有一个带有一些文本的文本框和一个已选中的复选框...您将保存为自定义文件类型,当您再次打开文件时,将选中该复选框并且文本框将具有文本。它基本上会将程序的状态保存为自定义文件类型。
例如 -> .pro、.lll、.hgy、.xyz、.abc
我只是好奇...这可能吗?如果可以,我将如何处理?
You can do what Ichiru states with a BinaryWriter and BinaryReader which I have done with some of my projects before using an in Memory datatable and serializing it.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using bs As New BinaryWriter(File.Open("Mydata.xyz", FileMode.Create))
bs.Write(TextBox1.Text)
bs.Write(CheckBox1.Checked)
bs.Close()
End Using
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
If File.Exists("Mydata.xyz") Then
Using br As New BinaryReader(File.Open("Mydata.xyz", FileMode.Open))
Try
TextBox1.Text = br.ReadString
CheckBox1.Checked = br.ReadBoolean
Catch ex As EndOfStreamException
'Catch any errors because file is incomplete
End Try
End Using
End If
End Sub
End Class
But .Net has a built in Settings Class that you can use to persist your Data. It would be used like this
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.MySettings.Default.checkbox1 = CheckBox1.Checked
My.MySettings.Default.textbox1 = TextBox1.Text
My.MySettings.Default.Save()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
CheckBox1.Checked = My.MySettings.Default.checkbox1
TextBox1.Text = My.MySettings.Default.textbox1
End Sub
End Class
This is not possible unless you have your system's default application setup to open with your executable that will read this custom-extension data file.
Custom extensions can't be executed like a .exe would be, but they can be read by a .exe and used to configure settings for that particular .exe
是的,可以创建自己的自定义文件类型。解决此类问题的最佳方法是创建二进制写入器 在二进制写入器中,您将写入文本框的内容和复选框的状态。
写作:
BinaryWriter.Write("string")
BinaryWriter.Write(false)
阅读:
String str
Boolean bool
str = BinaryReader.ReadString()
bool = BinaryReader.ReadBoolean()