473,659 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
strXMLComputerN ame = Mid(strConfigFi le, InStr(strConfig File,
"<chat_name >") + 11, InStr(strConfig File, "</chat_name") -
(InStr(strConfi gFile, "<chat_name >") + 11))

'-- Replace the default chat name with the pc name
strConfigFile = Replace(strConf igFile, strXMLComputerN ame,
strComputerName )

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

Nov 21 '05 #1
5 1413
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******@comca st.net> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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
strXMLComputerN ame = Mid(strConfigFi le, InStr(strConfig File,
"<chat_name >") + 11, InStr(strConfig File, "</chat_name") -
(InStr(strConfi gFile, "<chat_name >") + 11))

'-- Replace the default chat name with the pc name
strConfigFile = Replace(strConf igFile, strXMLComputerN ame,
strComputerName )

myStreamWriter = File.CreateText ("../Config.xml")
myStreamWriter. Write(strConfig File)
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 "selectsingleno de" method to find the node
Then set the nodes value using the node.value = [newvalue] method.

Hope this helps.

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

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

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

'get the value
Dim Node As Xml.XmlElement =
CType(xd.Docume ntElement.Selec tSingleNode( _

"/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.CreateElemen t("add")
Node.SetAttribu te("key", key)
Node.SetAttribu te("value", value)

'look for the appsettings node
Dim Root As Xml.XmlNode =
xd.DocumentElem ent.SelectSingl eNode("/configuration/appSettings")

'add the new child node (this key)
If Not Root Is Nothing Then
Root.AppendChil d(Node)
Else
Try
'appsettings node didn't exist, add it before adding the
new child
Root =
xd.DocumentElem ent.SelectSingl eNode("/configuration")
Root.AppendChil d(xd.CreateElem ent("appSetting s"))
Root =
xd.DocumentElem ent.SelectSingl eNode("/configuration/appSettings")
Root.AppendChil d(Node)
Catch ex As Exception
'failed adding node, throw an error
Throw New Exception("Coul d not set value", ex)
End Try
End If
End If

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

"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******@comca st.net> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.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
3539
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 text file i don't want to read empty lines(If the user as edit the file in notpad..), sense it makes commas at all new lines and then split the text at every comma in to an array. The array will contain empty "slot's" if there is a lot of commas in a...
3
4729
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 byte-strings instead of unicode strings. For example, if a file foo.txt contains some text encoded in latin1: >>> import codecs >>> f = codecs.open('foo.txt', 'r', 'utf-8', 'replace') >>>
3
4847
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 a process of notepad on the server , but does not open the file in notepad. What am I doing wrong? can you show me how to do it please? What I am doing here is : I have datagrid data and when clicked on linkbutton (logFile), it finds the row...
8
3627
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 text fields. Each date for example 10/31/03 or October 31st 2003 is stored as 10/31/A3 in the system. My reasoning for this is because they couldn't solve their Y2K problem or this is their solution to it. All dates prior to 2000 are stored...
8
4476
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 application): coder something.txt an_exsiting_file.txt, then if I chose to overwrite "an_exsiting_file.txt", an error message pops up, saying that to many files are open <EMFILE>. Any help would be appreciated. Here is the code (incomplete but still...
22
4973
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 connection with no problem. What is the correct way to open a .CSV file ? If I can not open the CSV file, how can I programmatically save the CSV file to an XLS file ? Thanks a lot. dim myCon OleDb.OleDbConnection
10
2069
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 names in a list (members). I open both input and output file in binary mode and do the transformation. However, I can't open the resulting file, Word just telling that there was an error. Does anybody what I am doing wrong? Oh, and is this...
6
6000
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 such easy as it seems to be. ;) I am using Python 2.5.2, by the way. I think this is a bug in at least in this version of python, but maybe StringIO isn't just file-like enough for this "korky" tarfile module. But this would conflict with its...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.