Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET - Creating a text file / StreamWriter question

Status
Not open for further replies.

DSect

Programmer
Sep 3, 2001
191
0
0
US
Hello - I'm a VB.NET novice and I'm making a little program that allows you to select a number of HTML files and it will go through and replace internet addresses with hyperlinks.

I am very close to getting it done, but I am totally confused on how to write the marked-up text to a file. I have the text in a variable, so it's just a matter of writing it at this point.

Here's my code:

Code:
    Private Sub btnAddFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFiles.Click
        Dim sReader As System.IO.StreamReader
        Dim arFiles As Array
        Dim strFileNames, strInStream, strOutStream As String
        Dim i As String
        diaOpenFile.ShowDialog()
        arFiles = diaOpenFile.FileNames
        For Each i In arFiles
            txtFileNames.Text = i ' to show me the file name(s) for testing
            sReader = New System.IO.StreamReader(i)
            strInStream = sReader.ReadToEnd
            strOutStream = InsertHyperlinks(strInStream)
            txtContents.Text = strOutStream ' to show me the output in a text box for testing
            ' Now What? I need to write to a file.. 
        Next

    End Sub

So - What I want is to write strOutStream to a text file and name the file i (i = the file that was opened) & "_PARSED".

I messed around with StreamWriter and I was able to create the file w/ the name I want but it was 0 bytes long (aka empty). I'm messing something up in getting the string to the stream.

Can anyone help me to write strOutStream to a text file and name it like I was doing?

Thanks again - I'm excited because this little proggy will make my life alot easier and it's my first totally-from-scratch creation that has a useful purpose.

Also - Any comments are welcomed in syntax / neat code, etc..
 
Use the StreamWriter object as shown below.
The write method of the streamWriter as shown will overwrite the specified file if it exists, without warning.
If you wanted to append (not relevant to this example) you would use the overloaded New constructor for the StreamWriter object and set the boolean 'append' parameter to true.

Code:
Dim strParsedFileName As String
strParsedFileName = System.IO.Path.GetDirectoryName(i) & "\" & _
                    System.IO.Path.GetFileNameWithoutExtension(i) & _
                    "_PARSED" & _
                    System.IO.Path.GetExtension(i)
Dim sWriter = New System.IO.StreamWriter(strParsedFileName)
sWriter.Write(strOutStream)
sWriter.Close()

-Stephen Paszt


 
Hi,

I want to write a tool that allows me just to replace one File by another.. Is it possible to use your examples for this?

It should work like, I'm executing the exe, the program replaces the given file, without asking.

I'm sorry, am a total newbie at VB. So I have no clue how to say VB what it has todo ;>
It would be very nice if someone could help me.

thx a lot.

RedNuth
 
RedNuth ,It's very very easy ,just use System.Io.File class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top