|
-
May 16th, 2011, 01:13 AM
#1
Thread Starter
New Member
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.
-
May 16th, 2011, 01:45 AM
#2
Fanatic Member
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 .
Last edited by aNubies; May 16th, 2011 at 01:50 AM.
-
May 16th, 2011, 02:16 AM
#3
Thread Starter
New Member
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. ???
Last edited by kizersouzay; May 16th, 2011 at 02:24 AM.
-
May 16th, 2011, 02:46 AM
#4
Lively Member
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.
If my post was helpful to you, then express your gratitude using Rate this Post.
To understand recursion, you must first understand recursion.
-
May 16th, 2011, 03:00 AM
#5
Fanatic Member
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.
-
May 16th, 2011, 03:59 AM
#6
Thread Starter
New Member
Re: How to save application data (input+output) in a new file format in vb
 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.
-
May 16th, 2011, 04:59 AM
#7
Fanatic Member
Re: How to save application data (input+output) in a new file format in vb
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 .
-
May 16th, 2011, 05:44 AM
#8
Thread Starter
New Member
Re: How to save application data (input+output) in a new file format in vb
Thanks aNubies. I will definitely check it out.
-
May 16th, 2011, 01:52 PM
#9
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.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
May 16th, 2011, 11:17 PM
#10
Thread Starter
New Member
Re: How to save application data (input+output) in a new file format in vb
 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
-
May 16th, 2011, 11:49 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|