Solved

Append text in vb.net

Posted on 2012-03-27
7
381 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
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.

 
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

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
Angular JS Route 3 54
Run time Error 4 34
Vb. Net application freezes 9 30
C# guarantee sql connection close 6 30
PaperPort is a popular document imaging/management product from Nuance Communications (http://www.nuance.com/). It is in widespread use by both individuals (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) and businesses (http:/…
*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…
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 video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …

920 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

13 Experts available now in Live!

Get 1:1 Help Now