Solved

Append text in vb.net

Posted on 2012-03-27
7
388 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 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 29

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 29

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
In this first video of the three-part Xpdf series, we introduce and describe Xpdf, a library containing nine command line utilities that perform various functions on PDF files. We show where the library is located and how to download it, discuss its…
In this third video of the Xpdf series, we discuss and demonstrate the PDFtoText utility, which converts PDF files into plain text files. Download and install the software.: You may have already downloaded and installed the Xpdf tools while watching…

828 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