Beginners needs some help in Writing to text file

M

Marc

Hi,

I have a button on a form that, when pressed, adds additional user
defined buttons to the form. I need to be able to save the user
preferences and thought the best way would be to write the information
to a text file.

Can anyone supply the code on how to write the newly created buttons
name to a text file. As more than 1 new button may be created I want to
store the information for each new button on a new line then read the
information back in.

Thanks

Marc
 
L

lord.zoltar

Marc said:
Hi,

I have a button on a form that, when pressed, adds additional user
defined buttons to the form. I need to be able to save the user
preferences and thought the best way would be to write the information
to a text file.

Can anyone supply the code on how to write the newly created buttons
name to a text file. As more than 1 new button may be created I want to
store the information for each new button on a new line then read the
information back in.

Thanks

Marc

You will probably want to look into the FileStream class.
See here:
http://msdn2.microsoft.com/en-us/library/system.io.filestream.aspx
 
G

Guest

just add

Imports System.IO

at the very top of the code window (above the Public Class Foo), then you
need to make FileStream and a StreamWriter objects

''where you want to write to the file...
''creates the objects used to write to a file
Dim fs As New FileStream("C:\Buttons.txt", FileMode.Create, FileAccess.Write)
Dim sw As New StreamWriter(fs)

''writeline is the method you want to write a single line to the file, if the
''buttons are in an array, just throw this in a loop
sw.WriteLine(Me.Button1.Name)

''VERY IMPORTANT: close the streams so the file will be usable after your app
''is done with it
sw.Close()
fs.Close()


hope this helps
 
G

Guest

Suggest one way is to create a class to hold your user preferences then
serialize this class to a file when the use exits your application then
de-serialize it back to the class when your application starts.
 
M

Marc

Thanks you very much..was most helpful!

I know have a text file where each line represents a button control
name. You dont knopw the code to read this file back in and to create a
button for each line do you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top