Solved

Append text in vb.net

Posted on 2012-03-27
7
392 Views
Last Modified: 2012-08-13
When I use file.appendtext("myfile"), it does not append right after the first line.
It looks like this

This is a test.

This is a test.

I want the result to be like this

This is a test.
This is a test.

I don't know in advance whether there will be  an extra "" line.
0
Comment
Question by:VBdotnet2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37773043
Is the carriage return in the original file?


If so the only way that I can see to get rid of it is to re-write the whole thing.
0
 

Author Comment

by:VBdotnet2005
ID: 37773060
What contains in a text file is this

Resultfile
accountnumber : 12345
/*

I want to remove  "/*" from my file, close a file and append new data from db to the same text file.

Right now it look like this

Resultfile
accountnumber : 12345

accountnumber : 67890
/*

instead of
Resultfile
accountnumber : 12345
accountnumber : 67890
/*
0
 

Author Comment

by:VBdotnet2005
ID: 37773072
This is my code.

dim value as string = file.readalltext(myfile)
value = value.replace("/*", "")
dim sw as new streamwriter(myfile)
sw.writeline(value)
sw.close
dim test as streamwriter = file.appendtext(myfile)
test.writeline("mydata")
test.close
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37773279
you also need to remove the new line.

Try this:
value.Remove(value.LastIndexOf(vbNewLine))
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 37773431
I would say that the code is far from perfect. Can you explain why do you
- open your file,
- replace ("/*", "")
- close file
 - open file immediately after that
-appebd text
-close file
?
Very expensive exercise in I/O

I'd do:
- open your file,
- replace ("/*", "mydata")
-close file

Code (didn't test)
dim value as string = file.readalltext(myfile)
' value = value.replace("/*", "") ' this line creates a blank line!!!
 value = value.replace("/*", "mydata") 
dim sw as new streamwriter(myfile)
sw.write(value)
sw.close

Open in new window

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 37773512
OK, I tested the code.

File "myfiletxt" content"

"accountnumber : 12345
/*"

        Dim value As String = File.ReadAllText("myfile.txt")
        ' value = value.replace("/*", "") ' this line creates a blank line!!!
        value = value.Replace("/*", "accountnumber : 67890")
        Dim sw As New StreamWriter("myfile.txt")
        sw.Write(value)
        sw.Close()
        ' Print to output window:
        Dim checkvalue As String = File.ReadAllText("myfile.txt")
        Debug.WriteLine(checkvalue)

Open in new window


Output:
accountnumber : 12345
accountnumber : 67890
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 37773971
Try following code which is generic to append text

Public Sub AppendText(File As String, TextToAppend As String)
Dim str As String = IO.File.ReadAllText(File)
'strip out empty lines
While str.Contains(VBCRLF & VBCRLF)
    str = str.Replace(VBCRLF & VBCRLF, VBCRLF)
End If
str = str.Replace("/*", "")
str = str & VBCRLF & TextToAppend & VBCRLF & "/*"
IO.File.WriteAllText(File, str)
End Sub

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Power PDF (http://www.nuance.com/for-business/document-imaging-and-scanning/power-pdf-converter/index.htm) is the newest product from the Document Imaging division of Nuance Communications (http://www.nuance.com/). It is available in two editions — …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

739 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