David Cáder
David Cáder

Reputation: 3

I need to use text from textfile in my application, and I cant ge it working

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

Answers (4)

Vashdev Heerani
Vashdev Heerani

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

Mojtaba Tajik
Mojtaba Tajik

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

Vashdev Heerani
Vashdev Heerani

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

Abra
Abra

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

Related Questions