Reputation: 3
I'm building application and I ran into a little problem. My application should show our workers, when they arrive to work and and where they headed. I have a datetime picker there and a every new day, when I fill out the application, it should create a new writeline, like this:
5th August Prague
6th August Pilsen etc...
I have only what I'm showing you and it creates a new textfile every time i try it. Please help.
This is all i have
StreamWriter dc = new
StreamWriter(@"C:\Users\dejv2\Desktop\Program Test\Docházka\David
Cáder"+ "Docházka David Cáder.txt");
if (textBox1.Text == "David Cáder")
{
dc.WriteLine(dateTimePicker1.Text + " - David Cáder - " +
textBox56.Text);
}
Upvotes: 0
Views: 77
Reputation: 679
FileStream fs = new FileStream(@"C:\Users\dejv2\Desktop\Program Test\Docházka\David
Cáder"+ "Docházka David Cáder.txt", FileMode.Append, FileAccess.Write);
StreamWriter dc = new StreamWriter(fs);
if (textBox1.Text == "David Cáder")
{
dc.WriteLine(dateTimePicker1.Text + " - David Cáder - " +
textBox56.Text);
}
dc.Close();
fs.Close();
Other solution using FileStream hopefully this will work as well
Upvotes: 1
Reputation: 1733
The simplest way :
var content = new List<string>();
if (textBox1.Text == "David Cáder")
{
content.Add(dateTimePicker1.Text + " - David Cáder - " + textBox56.Text)
}
File.AppendAllLines((@"C:\Users\dejv2\Desktop\Program Test\Docházka\DavidCáder" + "Docházka David Cáder.txt", content);
if you want to use StreamReader & StreamWriter be sure to close the file :
string content = string.Empty;
using (StreamReader sr = new StreamReader(@"C:\Users\dejv2\Desktop\Program Test\Docházka\DavidCáder" + "Docházka David Cáder.txt"))
{
content = sr.ReadToEnd();
}
using (StreamWriter dc = new StreamWriter(@"C:\Users\dejv2\Desktop\Program Test\Docházka\DavidCáder" + "Docházka David Cáder.txt"))
{
dc.WriteLine(content);
if (textBox1.Text == "David Cáder")
{
dc.WriteLine(dateTimePicker1.Text + " - David Cáder - " + textBox56.Text);
}
}
Upvotes: 0
Reputation: 679
StreamReader sr = new StreamReader(@"C:\Users\dejv2\Desktop\Program Test\Docházka\David
Cáder"+ "Docházka David Cáder.txt");
string str = sr.ReadToEnd();
sr.Close()
StreamWriter dc = new StreamWriter(@"C:\Users\dejv2\Desktop\Program Test\Docházka\David
Cáder"+ "Docházka David Cáder.txt");
dc.WriteLine(str);
if (textBox1.Text == "David Cáder")
{
dc.WriteLine(dateTimePicker1.Text + " - David Cáder - " +
textBox56.Text);
}
I have edit the code now hopefully it will work
Upvotes: 0
Reputation: 20914
The StreamWriter
constructor you are using always creates a new file. You can try using the constructor that allows you to append to an existing file.
Upvotes: 0