2012-12-01 45 views
1

是否有可能在VB程序中打開的自定義文件類型。是否有可能爲VB中的程序創建自定義文件類型

例如:有一個文本框,其中包含一些文本和已選中的複選框......您可以保存爲自定義文件類型,當您再次打開該文件時,將選中該複選框,並且文本框會有文字。它基本上將程序的狀態保存爲自定義文件類型。

E.g. - >的.pro,.lll,.hgy,名爲.xyz,名爲.abc

我只是好奇...這是可能的,如果是的話,我將如何處理這?

+0

使用這是的WinForms或WPF? –

+0

這是在WindowsForms –

+0

你想讓你的程序打開,當你雙擊這個文件或在你的程序啓動時讀取它? –

回答

2

你可以做什麼Ichiru與BinaryWriter規定和BinaryReader,這是我在使用內存數據表和序列化之前完成的一些項目。

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 

但.Net有一個內置的Settings Class,您可以使用它來保存您的數據。它會像這樣

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 

enter image description here

+0

好吧,保存部分工作..但我不能讓它再次開放。沒有任何錯誤,當我打開它時沒有任何變化。 –

1

是的,可以創建自己的自定義文件類型。 解決此類問題的最佳方法是創建一個二進制編寫器 在二進制編寫器中,您將編寫文本框的內容以及複選框的狀態。

寫作:

BinaryWriter.Write("string") 
BinaryWriter.Write(false) 

閱讀:

String str 
Boolean bool 
str = BinaryReader.ReadString() 
bool = BinaryReader.ReadBoolean() 
1

這是不可能的,除非你有你的系統中的默認應用程序設置你的可執行文件,會讀這個自定義擴展的數據文件打開。

自定義擴展無法執行像一個.exe文件會是這樣,但他們可以通過一個.exe文件讀取並用於爲特定的.exe配置設置

相關問題