VB.net: overwrite everything in a text file
up vote
-2
down vote
favorite
in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks
vb.net
|
show 1 more comment
up vote
-2
down vote
favorite
in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks
vb.net
You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32
as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37
2
You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40
Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03
Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30
|
show 1 more comment
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks
vb.net
in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks
vb.net
vb.net
asked Nov 21 at 17:30
user99170
1
1
You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32
as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37
2
You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40
Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03
Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30
|
show 1 more comment
You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32
as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37
2
You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40
Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03
Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30
You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32
You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32
as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37
as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37
2
2
You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40
You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40
Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03
Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03
Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30
Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
Read (ie: load) everything in the TXT file into your program.
Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"
Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)
'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum
Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).
'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)
'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Read (ie: load) everything in the TXT file into your program.
Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"
Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)
'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum
Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).
'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)
'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()
add a comment |
up vote
2
down vote
Read (ie: load) everything in the TXT file into your program.
Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"
Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)
'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum
Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).
'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)
'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()
add a comment |
up vote
2
down vote
up vote
2
down vote
Read (ie: load) everything in the TXT file into your program.
Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"
Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)
'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum
Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).
'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)
'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()
Read (ie: load) everything in the TXT file into your program.
Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"
Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)
'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum
Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).
'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)
'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()
answered Nov 22 at 16:03
ThePeter
308111
308111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417651%2fvb-net-overwrite-everything-in-a-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32
as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37
2
You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40
Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03
Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30