Open a text file and replace text help

  • Thread starter Thread starter Jim
  • Start date Start date
J

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()
 
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
 
Chris

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


Jim
 
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)
 
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?
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top