Write Function

PureSc0pe

Well-known member
Joined
Mar 17, 2004
Messages
161
Location
Arizona
This function is used like this: TextBox1.Text = 32

cfg.write("Set Example_1", TextBox1.Text)

It Writes to the file exactly like this:

Set Example_1 "32"

What I need to know is how to make it change only whats in the "" when its saving the same Example (Set Example_1) instead of just creating a whole new line.

Code:
Public path As String = Application.StartupPath & "\config.cfg"

    Public Function Write(ByVal Name As String, ByVal Value As String) As Boolean

        Dim fs As StreamWriter

        If File.Exists(path) = False Then
             Create the file.
            fs = File.CreateText(path)
            fs.Write(Name & " """ & Value & """" & vbCrLf)
            fs.Close()
        Else
            fs = File.AppendText(path)
            fs.Write(Name & " """ & Value & """" & vbCrLf)
            fs.Close()
        End If
    End Function
 
imports system.IO


Dim path As String = Application.StartupPath & "\config.cfg"
Dim SW As StreamWriter
Dim FS As FileStream
FS = New FileStream(path, FileMode.Create)
SW = New StreamWriter(FS)
SW.WriteLine("Set Example_1", TextBox1.Text)
SW.WriteLine()
SW.WriteLine("BLAH BLAH BLAH NEXT LINE BLAH")
SW.Close()
FS.Close()

----

this should help you out, remember at the top of the file to import system.io
 
Last edited by a moderator:
I dont think you see what Im trying to do...Im using a class to call the write function because I have numerous things to write. The class I have does everything I need it to except it writes a new line everytime you write. I need it to alter the value in the "" when it applies.
 

Attachments

Im alittle confused you mean

1. You want the output file to only have one line of text in it at all times


or


2. you want lots of lines in the file each with a new whatever is in the ""


Type an example of how you want the output file to look like exactly like after a few writes.


If your only wanting one line to always be present in the file then you could always delete the file at the beginning of the function. that way only one line of data is always written.


Public path As String = Application.StartupPath & "\config.cfg"

Public Function Write(ByVal Name As String, ByVal Value As String) As Boolean

Dim fs As StreamWriter

If File.Exists(path) = False Then
Create the file.
fs = File.CreateText(path)
fs.Write(Name & " """ & Value & """" & vbCrLf)
fs.Close()
Else
File.Delete(path)
fs = File.CreateText(path)
fs.Write(Name & " """ & Value & """" & vbCrLf)
fs.Close()

End If
End Function
 
Last edited by a moderator:
If its writing to the same name then I need it to only change the value in the " " and not make a new line. Thats all. It will make a new line only if the same name in the file is not already there.

Example of the config.cfg file:

Set Settings_1 "32"
Set Settings_2 "12"
Set Settings_3 "1"


If I write using this: cfg.write("Set Settings_1", TextBox.Text) and TextBox1.Text = 16 then the file should now look like this:

Set Settings_1 "16"
Set Settings_2 "12"
Set Settings_3 "1"
 
Back
Top