TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
matthew king
NA
52
0
Streamreader
Oct 19 2009 3:26 PM
Sir /Ma'am,
I'm trying to use streamreader to query a .txt file and change a specific line based on a user input. If the line does not exist, I'm trying to append the data to the bottom of the .txt file. So, the user inputs the data into textbox3 and pushes a button. Upon pushing the button, the code will search c:\test.txt to look for a line beginning with the word entered into textbox3. If it finds it, it simply overrides that line with the value the user entered into textbox2. If the current code doesn't find a line beginning with the word entered into textbox3, it copies the entire .txt file and appends it to the bottom of test.txt. However, as stated above, I just want that one line appended to the buttom, not the entire file. As always, thank you for your assistance!
private void button3_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string line = string.Empty;
// path of the file
using (StreamReader fs = new StreamReader(@"C:\test.txt", true))
{
// checks until end of file
while ((line = fs.ReadLine()) != null)
{
// This will serch a txt file for the word payload
if (line.StartsWith(textBox3.Text))
//This will override the sentence....
sb.AppendLine(textBox3.Text + ", " + textBox2.Text);
else
sb.AppendLine(line);
}
}
// path of file that you want to write,
// and whether you want it to append to the file or not
// I chose not to append.
using (StreamWriter sw = new StreamWriter(@"C:\test.txt", true))
{
sw.Write(sb.ToString());
}
}
}
}
Reply
Answers (
6
)
No overwriting by streamwriter
Save DataGridView to XML using LINQ