By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

Open a text file and replace text help

P: n/a
Jim
Below is my code, I'm opening an xml file and I have a chat name node
within it. By default the node has a value of "mypc" for which I'm
replacing and putting in a new value. I don't know if this is the best
approach but I'm trying it. I can open the stream fine and replace the
text. But when I try to write the stream back, I get an error saying
the file is in use by another process. Any ideas? Thanks in advance

myStreamReader = File.OpenText("../Config.xml")
strConfigFile = myStreamReader.ReadToEnd()
myStreamReader.Close()
'-- Get the computer name from the XML
strXMLComputerName = Mid(strConfigFile, InStr(strConfigFile,
"<chat_name>") + 11, InStr(strConfigFile, "</chat_name") -
(InStr(strConfigFile, "<chat_name>") + 11))

'-- Replace the default chat name with the pc name
strConfigFile = Replace(strConfigFile, strXMLComputerName,
strComputerName)

myStreamWriter = File.CreateText("../Config.xml")
myStreamWriter.Write(strConfigFile)
myStreamWriter.Close()

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Yes, you are the one with the file open. Try doing a myStreamReader.Close
after you are done using it. There are XMLStreamReader classes (not
positive that's the name of it) that you could look to use.

Chris

"Jim" <je******@comcast.net> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Below is my code, I'm opening an xml file and I have a chat name node
within it. By default the node has a value of "mypc" for which I'm
replacing and putting in a new value. I don't know if this is the best
approach but I'm trying it. I can open the stream fine and replace the
text. But when I try to write the stream back, I get an error saying
the file is in use by another process. Any ideas? Thanks in advance

myStreamReader = File.OpenText("../Config.xml")
strConfigFile = myStreamReader.ReadToEnd()
myStreamReader.Close()
'-- Get the computer name from the XML
strXMLComputerName = Mid(strConfigFile, InStr(strConfigFile,
"<chat_name>") + 11, InStr(strConfigFile, "</chat_name") -
(InStr(strConfigFile, "<chat_name>") + 11))

'-- Replace the default chat name with the pc name
strConfigFile = Replace(strConfigFile, strXMLComputerName,
strComputerName)

myStreamWriter = File.CreateText("../Config.xml")
myStreamWriter.Write(strConfigFile)
myStreamWriter.Close()

Nov 21 '05 #2

P: n/a
Jim
Chris

Right after I read the stream into the String variable I close it
strConfigFile = myStreamReader.ReadToEnd()
myStreamReader.Close()
Jim

Nov 21 '05 #3

P: n/a
Jim,

Try to use this. I used this to edit a .config file I used in another
application. Open the file as a xmlDocument, make the change to the node and
save it.

Use the "selectsinglenode" method to find the node
Then set the nodes value using the node.value = [newvalue] method.

Hope this helps.

------CODE BEGINS-------

Public Function SaveSetting(ByVal key As String, ByVal value As String) As
Boolean

Dim xd As New Xml.XmlDocument
'load the xml file
xd.Load(appConfigFile)

'get the value
Dim Node As Xml.XmlElement =
CType(xd.DocumentElement.SelectSingleNode( _

"/configuration/appSettings/add[@key=""" & _
key & """]"), Xml.XmlElement)
If Not Node Is Nothing Then
'key found, set the value
Node.Value = value
Else
'key not found, create it if you want or remove and throw error
Node = xd.CreateElement("add")
Node.SetAttribute("key", key)
Node.SetAttribute("value", value)

'look for the appsettings node
Dim Root As Xml.XmlNode =
xd.DocumentElement.SelectSingleNode("/configuration/appSettings")

'add the new child node (this key)
If Not Root Is Nothing Then
Root.AppendChild(Node)
Else
Try
'appsettings node didn't exist, add it before adding the
new child
Root =
xd.DocumentElement.SelectSingleNode("/configuration")
Root.AppendChild(xd.CreateElement("appSettings"))
Root =
xd.DocumentElement.SelectSingleNode("/configuration/appSettings")
Root.AppendChild(Node)
Catch ex As Exception
'failed adding node, throw an error
Throw New Exception("Could not set value", ex)
End Try
End If
End If

'finally, save the new version of the config file
xd.Save(appConfigFile)

"Jim" wrote:
Chris

Right after I read the stream into the String variable I close it
strConfigFile = myStreamReader.ReadToEnd()
myStreamReader.Close()
Jim

Nov 21 '05 #4

P: n/a
Man, I'm having an off day... Read it twice looking for a close and didn't
see it <sigh> That should do the trick really. As I was just reading
about, you might want to use dispose instead of close if you're done with
the file.

Chris
Are you sure no other application has the file open?
"Jim" <je******@comcast.net> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Chris

Right after I read the stream into the String variable I close it
strConfigFile = myStreamReader.ReadToEnd()
myStreamReader.Close()
Jim

Nov 21 '05 #5

P: n/a
Jim
Thanks for the suggestions all...I tried the code above where I'm just
loading the single node and modifying it. That helped minimize the
amount of code, but when I execute the .Save, I still get a message
saying the file is in use by another process. I even rebooted to
ensure nothing else was holding a reference and I still get this
message. Any other ideas would be appreciated.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.