Solved

Append text in vb.net

Posted on 2012-03-27
7
379 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
you also need to remove the new line.

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

Expert Comment

by:anarki_jimbel
Comment Utility
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 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

*Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…

763 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

7 Experts available now in Live!

Get 1:1 Help Now