[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Open a file - replace 1 word - close a file

Posted on 2011-05-10
Medium Priority
Last Modified: 2012-05-11
In my under - sized brain this code should work.

 If File.Exists("C:\KeyPatroller\SQL\CorporateRemote.sql") Then

            fStream = New FileStream("C:\KeyPatroller\SQL\CorporateRemote.sql", FileMode.Open, FileAccess.ReadWrite)

            sReader = New StreamReader(fStream)

            sReader.ReadToEnd.Replace("keypatrollerremote", "Bob")


        End If
Question by:MBoy
1 Comment
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 35732709
This line:

    sReader.ReadToEnd.Replace("keypatrollerremote", "Bob")

Creates a string with the word replaced...but that string is not "linked" to your file at all.  The string is simply garbage collected when the sub/function exits.

You need to store that modified string and then OVERWRITE the old file:
Dim FileName As String = "C:\KeyPatroller\SQL\CorporateRemote.sql"
        If File.Exists(FileName) Then
            Dim contents As String = My.Computer.FileSystem.ReadAllText(FileName)
            contents = contents.Replace("keypatrollerremote", "Bob")
            My.Computer.FileSystem.WriteAllText(FileName, contents, False)
        End If

Open in new window


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question