Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

Append text in vb.net

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.
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

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.
Avatar of VBdotnet2005

ASKER

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
/*
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
you also need to remove the new line.

Try this:
value.Remove(value.LastIndexOf(vbNewLine))
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

SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial