473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Open a text file and replace text help

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
5 1402
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BadOmen | last post by:
I have a text file that I want to save from my program. But I don't want to save the empty lines. I want to delete everything after the last character, Is that possible? Then when I read the...
3
by: Eric Brunel | last post by:
Hi all, I just found a problem in the xreadlines method/module when used with codecs.open: the codec specified in the open does not seem to be taken into account by xreadlines which also returns...
3
by: RN Das | last post by:
Hi expert, I have a small web application in C# and ASP.Net and I want to open a file in notepad when clicked on a linked button. Here is hander code when clicked on the link button. It can open...
8
by: John Wildes | last post by:
Hello all I'm going to try and be brief with my question, please tell me if I have the wrong group. We are querying transaction data from a DB3 database application. The dates are stored as...
8
by: someone | last post by:
I'm making an program that encodes text, and then writes it into another text file. The problem is that I can't seem to get the "file exists" checking right. If I use the command (coder is the...
22
by: fniles | last post by:
I have a .CSV file (comma delimited) that I want to open using OLEDB, but I get the error "External table is not in the expected format." If I save the .CSV file to an .XLS file, I can open the...
10
by: Antoine De Groote | last post by:
Hi there, I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.