Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


save and open file


save and open file

Author
Message
mell
mell
Forum God
Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)

Group: Forum Members
Posts: 9, Visits: 1
hi guys,
can anybody help me to solve my problems?
i'm new in vb .net and need your suggestions...

how to save the contains of the listbox and how to open that contains back again in that list box.
and what file extension it is used to save that type of file?

i'm doing my final project now and need your reply as soon as possible..

thx in advanced
Jim.S
Jim.S
Forum God
Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)

Group: Forum Members
Posts: 21, Visits: 1

Mell,

There are a few things to cover with this problem.  First off, the file extension does not matter as far as your program is concerned.  Any file that you create and write to using the FileSystem in vb.net will create an ANSI text file that can be read just like a normal text file from notepad.  You as the programmer have the option to change the extension if you want, but the contents will not change.  Extensions are only there as footnotes to programs as to how they should interpret the data, they don't actually change the data within the file.

Knowing that, all you need to do is step through the contents of your listbox with a loop and print each line to a file.  In order to do this, use the above mentioned FileSystem inherent in vb.net.  There are some syntax trickeries here.

FileSystem.FileOpen(filenumber, path, openmode)
This will open an existing file or create a new one if one does not exist.
filenumber - An integer value that will be the designation of the file for read/write operations.
path - The full path to the file, including the complete filename and extension.
openmode - a constant representing the method to access the file (input, output, append, binary, etc...)
After you have opened the file, use FileSystem.PrintLine(filenumber, value) command to write to the file.
This will write whatever value is to the next line of the file.
When you are done writing to the file, close it with FileSystem.FileClose(filenumber).
You now have a file on the drive with the data that you wrote to it.  You can open it in notepad to see what it looks like, and you may have to do some testing to get it just right.

To read from the file is very similar, open it with FileSystem.FileOpen, but set the mode to input and use FileSystem.LineInput(FileNumber) to get each line of information.  Again, make sure you test this a lot so you can get it just right.  Each time you use the LineInput function, it will input one line from the file, so you may have to check against the FileSystem.EOF(FileNumber) property to see if you've reached the end of the file.  Also, LineInput is a function, so you will have to set it against a string variable: as in varString = FileSystem.LineInput(FileNumber).

I think I've covered everything, if I haven't let me know.  By the way, the FileSystem object can be a very complex animal.  What I've shown you here is the very basics of how to use it, but I've found that manipulating the basics can sometimes give you better results than the really complex functions.  You just have to write more code.

Best of Luck,

Jim




Jim@nationalelectric.com


mell
mell
Forum God
Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)

Group: Forum Members
Posts: 9, Visits: 1
thank you very much..
will try it and let you know if anything wrong happens...
thx again..
mell
mell
Forum God
Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)Forum God (1.1K reputation)

Group: Forum Members
Posts: 9, Visits: 1
another question...
how if i wanted to save the content of a textbox and open/display it back?
would it be the same as for the listbox?

there is saveFileDialog built in function, how does it work?
and if we save and open the file like that, does it have anythin to do with database?

thx
cmos
cmos
Supreme Being
Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)Supreme Being (211 reputation)

Group: Forum Members
Posts: 1, Visits: 1
Thx for this very usefull topic!

I did:

Dim FileNumber As Int16


'FileSystem.FileOpen(FileNumber, "d:\to\tteste.txt", OpenMode.Output)

FileSystem.PrintLine(FileNumber, "ssss 1")


And I got __

An unhandled exception of type 'System.IO.IOException' occurred in microsoft.visualbasic.dll

Additional information: Bad file name or number.
__

But the path its correct!!!

can u give me a Tip?

Thanks.
Jim.S
Jim.S
Forum God
Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)Forum God (1.3K reputation)

Group: Forum Members
Posts: 21, Visits: 1

Mell:

I'm not too sure about the saveFileDialog function off the top of my head, but its probably not what you need.  The textbox will be the same as the listbox, all you are doing is capturing the data that is in the object and writing it to a file.  The type of object is not important, only the data.  Also, basic file IO operations don't have anything to do with databases, they are more simple than that.

 

cmos:

As for your issue, try assigning a value to your filenumber variable, or just use a solid value instead of a variable.  For example:

FileSystem.FileOpen(1, "d:\to\tteste.txt", OpenMode.Output)

FileSystem.PrintLine(1, "ssss 1")

Give that a shot.

Jim




Jim@nationalelectric.com


GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search