How to save application data (input+output) in a new file format in vb
I work in an engineering design house and programming is not my actual profession. And I am relatively new to VB .net (previous i had worked in vb 6)
I am working on an in house application. I am coding a simple application in vb .net. This applications takes a few inputs from user and calculates a result. Now I want to implement a simple functionality that user should be able to save the input and results just like most of the windows programs can in a new file format. And user should be able to open the save file when desired.
I can open a file and right text data to it.
But I have no clue how to implement this properly.
Please point me to some book or web site or topic in vb .net that I should dig into.
Re: How to save application data (input+output) in a new file format in vb
If you're trying to write something into a textfile then you can do something like this.
Code:
Dim newStreamWriter As New IO.StreamWriter("filepath")
newStreamWriter.WriteLine("text to be right")
newStreamWriter.Close()
BTW, Welcome to the Forum :D.
Re: How to save application data (input+output) in a new file format in vb
As I said earlier, I have some idea about writing text to files, but
I am not talking about text here.
Is it possible that I store the data in a structure and then write it to a file, just like text.
It may work but doesnt seem to be the right method. ???
:rolleyes:
Re: How to save application data (input+output) in a new file format in vb
Yes it is possible,but I'll not suggest to do that way.
The old gwbasic/Pascal have such feautre, but it is not so good...
Try this(not sure):
vb Code:
...
newStreamWriter.WriteLine(YourStructuredVariable)
...
I'll suggest save your data 'logically' into text file and read the same way.
Re: How to save application data (input+output) in a new file format in vb
if you want to you can also save your structure into a Binary file using binary formatter.
Re: How to save application data (input+output) in a new file format in vb
Quote:
Originally Posted by
S0LARIS
Yes it is possible,but I'll not suggest to do that way.
The old gwbasic/Pascal have such feautre, but it is not so good...
Try this(not sure):
vb Code:
...
newStreamWriter.WriteLine(YourStructuredVariable)
...
I'll suggest save your data 'logically' into text file and read the same way.
Well I was thinking the same but is there any other more professional method to handle this task?
Also I want to make a new windows file format with a new extension, so that when a user clicks a file with that extension it should automatically open in my application. Howz that possible?
This is a pretty standard function that almost all the professional applications have. I thought I would get the solution very soon at this forum.
A more professitional approach that I have found out by doing some search on the internet is to use XML. But It will take me a lot of time to go through it as I donno what is XML, so I need something quick.
One thing I have noticed that data when stored in a file with a structure as mentioned by Solaris, it is easilty readable if you open that file in a text editor. Which I dont want to.
Re: How to save application data (input+output) in a new file format in vb
Quote:
One thing I have noticed that data when stored in a file with a structure as mentioned by Solaris, it is easilty readable if you open that file in a text editor. Which I dont want to.
Then just like I said you can do the Binary Formater (serialization and deserialization) so it would be hard for them to read your data.
Code:
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
' This is for serialization
Dim fs As New FileStream("C:\somefile.dat", FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter()
Call bf.Serialize(fs, test)
fs.Close()
' This is for deserialization
Dim fs As New FileStream("C:\somefile.dat", FileMode.Open)
Dim bf As New BinaryFormatter()
Dim stct As test = Ctype(bf.Deserialize(), test)
fs.Close()
Well this is the least I can do, I think someone has a better and quick way for it you just need to wait them. But for now if you interested in trying that feel free to try it :D.
Re: How to save application data (input+output) in a new file format in vb
Thanks aNubies. I will definitely check it out.
Re: How to save application data (input+output) in a new file format in vb
The binary formatter is the quick and dirty way. Be aware that if you change the object being serialized to a binary representation, 'old' files written out may not be able to be read.
The 'proper' way would be to design and document a file format/structure and write a routine/object to write the file with that format. The format completely depends on the data being written, but the advantage of designing a format means you could have the freedom to write text, meta-data and binary data as you see fit, as long as it adheres to the design.
Re: How to save application data (input+output) in a new file format in vb
Quote:
Originally Posted by
SJWhiteley
The binary formatter is the quick and dirty way. Be aware that if you change the object being serialized to a binary representation, 'old' files written out may not be able to be read.
The 'proper' way would be to design and document a file format/structure and write a routine/object to write the file with that format. The format completely depends on the data being written, but the advantage of designing a format means you could have the freedom to write text, meta-data and binary data as you see fit, as long as it adheres to the design.
So can you please point me to some guide/reference? some example would be helpful.
Thanks
Re: How to save application data (input+output) in a new file format in vb
I concur with SJWhiteley. You need to first decide exactly what it is and exactly how you want the data stored. You can then use a BinaryWriter to write binary data to a file and a BinaryReader to read binary data from a file. They provide methods for working with standard .NET types, i.e. you can read and write Integers, Strings, Booleans, DateTimes, Bytes (singular and arrays) and more.
Let's say that you decided that your file format would be a String followed by two Integers. You could write out a file like this:
vb.net Code:
Private Sub WriteToFile(filePath As String, text As String, number1 As Integer, number2 As Integer)
Using file As New FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write),
writer As New BinaryWriter(file)
With writer
.Write(text)
.Write(number1)
.Write(number2)
End With
End Using
End Sub
Private Sub ReadFromFile(filePath As String, ByRef text As String, ByRef number1 As Integer, ByRef number2 As Integer)
Using file As New FileStream(filePath, FileMode.Open, FileAccess.Read),
reader As New BinaryReader(file)
With reader
text = .ReadString()
number1 = .ReadInt32()
number2 = .ReadInt32()
End With
End Using
End Sub
Note that the second method might throw an exception if the file is in the wrong format or it might just return nonsensical data. For that reason, it might not be a bad idea to define a small header that all files of your format have. It still doesn't guarantee anything but it makes telling the difference between a corrupt file and one in the wrong format easier.