I developed a vb.net standalone application and everything is working fine. Now i want to save the data on the form
as a file ie. like a word document with a custom extension. But when i try to open the file it should open as vb.net
form ie. as a runtime executable form. So when i run the application and click save button, each time a new file has to be created and the data should save in that file. When i open the file, all the saved data should appear in the
textboxes and it should be as a vb.net form.
I am unable to do this. Any help is greatly appreciated.
Well what you have to do if I am understanding you correctly is register the file extension to your exe. There are registery entries for each registered file type that tell windows what program should be used to open this type of file. Your program would have to be there under the extension you wish to use. You can do this through windows explorer tools menu, or the open with options or the registery.
Of course you have to write the code to write to the file, read from the file and populate your form as well as code to accept incoming parameters.
The proper way to do it is to make use of an installer. This can set all the File Associations for you once the program is installed. It is a bit more professional at the end of the day as well. It does everything the Great DataMiser mentioned
You'd just need to add a Setup project to your existing project, then follow these steps :
Open up your solution in Visual studio.
Add a Setup Project to your solution by clicking on File -> Add Project ->New project -> Setup & Deployment Projects ->Setup project
Right-click on your Setup Project in Solution Explorer and Select view, then select file types.
Right-click on "File types on target machine". Click Add "File Type"
You will notice that "New document Type#1" was added, and "&open" item underneath it.
The "new document type#1" can be anything you want - change it to something descriptive. Be careful not to overwrite existing file types!
In the "properties" window for new document type#1, you will need to change a few properties.:
Command:Change to the application that you want to run. ADD YOUR OUTPUT FILE
Description: This is the description of the file type
Extensions:This your list of extensions for you chosen Program. For example htg Separate each one with a ","
Icon:This will associate the icon with your file type,This shows up in the window explorer.
Click on &open. This is an action that is available if your right-click on the file.The default action("&Open" is currently set as the default) is what happens when you double click on the file. Right click on your "New document type#1" to add actions,but for the moment,lets define our "&open" action
Click on "&Open".You will see in the properties window "Name","Arguments","Verbs". Verb is hidden from the user,but is the key that is stored in the registry.Leave it same as the name,But without the "&".
This is the Setup project part. Now, obviously, you'd need to add the Open parameter to your project. The best way is through a Sub Main procedure :
It will look something like :
Code:
Imports System.IO
Module Module1
Sub Main(ByVal args() As String)
If args.Length > 0 Then
Application.Run(New frmHTG_Associate(args(0)))
Else
Application.Run(New frmHTG_Associate())
End If
End Sub
End Module
Now, when you navigate to a htg file, you will see the appropriate icon, and when you double click on it, it should open that file as well.
I am attaching these samples that can help you get started. In my main project, I do have a Save and Open menu :
Code:
Imports System.IO
Public Class frmHTG_Associate
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
'Write data to file
Dim myWriter As StreamWriter
Dim myStream As FileStream
myStream = New FileStream(("c:\File1.htg"), FileMode.Create)
myWriter = New StreamWriter(myStream)
Dim myItem As Object
For Each myItem In rtbHTG_Associate.Text
myWriter.Write(myItem.ToString & Environment.NewLine)
Next
myWriter.Close()
myStream.Close()
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'rtbHTG_Associate.LoadFile("c:\File1.htg", RichTextBoxStreamType.PlainText)
Dim textline As String
FileOpen(1, "c:\File1.htg", OpenMode.Input)
While Not EOF(1)
textline = LineInput(1)
rtbHTG_Associate.Text = rtbHTG_Associate.Text + textline + Chr(13) + Chr(10)
End While
End Sub
End Class
Hannes...thanks for explaining step by step very clearly and it works great. My problem is resolved half way. Saving the file with our
custom extension is perfect. But while opening, i need to go to the location of the file where it is saved and double click it and it should
open automatically as a vb.net form with the data in it.
rockers, there is something seriously wrong with me these days....
This is the second thread on this forum, in the past week, where I forget everything I want to write in a reply...
Anyways. With a shock I realised that my program is incomplete - I am so sorry about that Blame it on age or on a burn out.
Everything I mentioned was correct, we just need to tell our program to load the file that was double clicked. The obvious place to include that is Form_Load. I added these now :
Code:
'This is a function to to get the filename used
Private Function GetDoubleClickedFileName(ByVal StrFullPath As String) As String
Dim intPos As Integer
intPos = InStrRev(StrFullPath, "\") 'Get last Index of "\"
GetDoubleClickedFileName = Mid$(StrFullPath, intPos + 1) 'Get the full name
End Function
Private Sub OpenFilesByDoubleClicking()
Dim strFileExt As String 'Get File Extension
Dim strFileContents As String 'Get Contents of File
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs 'Get Command Line Arguments ( &open )
For i As Integer = 0 To CommandLineArgs.Count - 1 'Loop through all arguments
strFileExt = System.IO.Path.GetExtension(CommandLineArgs(i)) 'Get the filename extension
Select Case strFileExt
Case ".htg" 'This is to open a file with associated extension *.htg
strFileContents = My.Computer.FileSystem.ReadAllText(CommandLineArgs(i)) 'Read content
rtbHTG_Associate.Text = strFileContents 'Load content into RTB
Me.Text = GetDoubleClickedFileName(CommandLineArgs(i)) & " - HTG_AssociateFiles" 'Name of program + file opened
Case Else 'This is to open any other file extensions that its extension not .htg
'Just including it here, in case you have more than one variant of the extension you want
strFileContents = My.Computer.FileSystem.ReadAllText(CommandLineArgs(i))
rtbHTG_Associate.Text = strFileContents
Me.Text = GetDoubleClickedFileName(CommandLineArgs(i)) & " - HTG_AssociateFiles" 'Name of program + file opened
End Select
Next
End Sub
Private Sub frmHTG_Associate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
OpenFilesByDoubleClicking() 'Open from command line ( Double clicking )
End Sub
Uninstall the previous version of the program.
Build the program and setup that I am giving you now, and double click a saved file from My Computer. Voila! It works like a charm!!
Please let me know if you come right or if you do not understand any of my logic
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.