Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# save txt file on desktop

How can I save a txt file that it was created by me on desktop?

This is the code:

void CreaTxtBtnClick(object sender, EventArgs e){
    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    filePath = filePath + @"\Error Log\";
    TextWriter sw = new StreamWriter(@"Gara.txt");

    int rowcount = dataGridView1.Rows.Count;
    for(int i = 0; i < rowcount - 1; i++){
        sw.WriteLine(
            dataGridView1.Rows[i].Cells[0].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[1].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[2].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[3].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[4].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[5].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[6].Value.ToString() + '\t' +
            dataGridView1.Rows[i].Cells[7].Value.ToString() + '\t'
        );
    }
    sw.Close();
    MessageBox.Show("File txt creato correttamente");
}

I thought that with these instructions

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
filePath = filePath + @"\Error Log\";
TextWriter sw = new StreamWriter(@"Gara.txt");

I can save the file on Desktop, but the txt is created correctly in the wrong path. How can I fix it?

like image 955
Matteo Avatar asked Nov 24 '16 08:11

Matteo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

You've constructed your filePath, but haven't used it in TextWriter. Instead you're just writing to Gara.txt file which is by default located in the folder where your application started in.

Change your code to something like:

 filePath = filePath +@"\Error Log\Gara.txt";
 TextWriter sw= new StreamWriter(filePath);
like image 85
Andrey Korneyev Avatar answered Sep 19 '22 18:09

Andrey Korneyev