0

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);

        }

4 Answers4

1
        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

Vashdev Heerani
  • 660
  • 7
  • 21
0

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.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • @DavidCáder there are examples in the documentation page that I provided the link to. Here is one example copied from that Web page: `StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8, 512)` – Abra Aug 09 '19 at 05:31
0
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

Vashdev Heerani
  • 660
  • 7
  • 21
0

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);
        }
    }
Mojtaba Tajik
  • 1,725
  • 16
  • 34