Home All Groups Group Topic Archive Search About

Beginners needs some help in Writing to text file

Author
24 Nov 2006 3:33 PM
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

Author
24 Nov 2006 3:44 PM
lord.zoltar
Marc wrote:
Show quoteHide quote
> 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
Author
24 Nov 2006 3:57 PM
iwdu15
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

--
-iwdu15
Author
24 Nov 2006 5:40 PM
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


iwdu15 wrote:
Show quoteHide quote
> 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
>
> --
> -iwdu15
Author
24 Nov 2006 4:19 PM
Dennis
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.
--
Dennis in Houston


Show quoteHide quote
"Marc" wrote:

> 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
>
>