Solved

editing a text file

Posted on 2009-07-09
3
146 Views
Last Modified: 2012-05-07
Hello all

I have an application that reads a text file, i now have a set of circumstances whereby i need to change the text of the file on the fly from within my appication. I read the file as follows
"           If IO.File.Exists("C:\somthing.txt") Then
                Dim srCon As IO.StreamReader
                srCon = New System.IO.StreamReader(("C:\somthing.txt")
                While srCon.Peek <> -1
                    strLine = srCon.ReadLine
                    If strLine.StartsWith("server") Then
                        strLine = strLine.Substring(7)
                        strSrv = Trim(strLine)
                    ElseIf strLine.StartsWith("database") Then
                        strLine = strLine.Substring(9)
                        strDB = Trim(strLine)
                    ElseIf strLine.StartsWith("company") Then
                        strLine = strLine.Substring(8)
                        strSagepath = Trim(strLine)
                    ElseIf strLine.StartsWith("license") Then
                        strLine = strLine.Substring(8)
                        strLicense = Trim(strLine)
                    ElseIf strLine.StartsWith("printer1") Then
                        strLine = strLine.Substring(9)
                        strprinter1 = Trim(strLine)
                    ElseIf strLine.StartsWith("printer2") Then
                        strLine = strLine.Substring(9)
                        strprinter2 = Trim(strLine)
                    ElseIf strLine.StartsWith("printer3") Then
                        strLine = strLine.Substring(9)
                        strprinter3 = Trim(strLine)
                    End If  "

I know the lines i need to edit so i can do somthing like "If strLine.StartsWith("printer3") Then"
How can i edit the rest of that line after printer3
0
Comment
Question by:bostonste
3 Comments
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 500 total points
ID: 24812933
You'll want to create a system.io.streamwriter to do your writes.
dim sw as new system.io.streamwriter(different temporary file path)
sw.writeline(your manipulated text from the reader)

after you're done, overwrite your original file with the temporary file you wrote by using io.file.copy or io.file.move
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24813033
that file looks almost like an INI file - make a couple of changes (stick "=" between the param name & value and give the file a section header "[SectionName]") and you could just use an INI file reader like this one http://www.mentalis.org/soft/class.qpx?id=6 - makes your job a whole lot easier than parsing each line in the file until you get to the line you want.

Or convert it to XML and use the .Net XML classes.

If you do use a streamwriter to write back to the file - remember that you will need to write back the entire contents of the file not just the line you are changing. So for even a small change you will have to read in every line of the file.
0
 

Author Closing Comment

by:bostonste
ID: 31601532
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Recommendation vb6 to vb.net or others 14 109
Added a column screws up code 5 49
C# Offline Apllication 5 54
Advice on Xojo as a development tool over VB. 4 39
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now