Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Append text in vb.net

Posted on 2012-03-27
7
Medium Priority
?
405 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
Industry Leaders: 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 1000 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 1000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe INTRODUCTION This article presents a solution to a question (http://www.experts-exchange.com/Programming/Installation/Q_28396542.html)…
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.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

730 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