Mansoor Ahmad
Mansoor Ahmad

Reputation: 55

C#: StreamWriter program is not writing to text file

I know this question has probably been answered multiple times across this site, but even after looking at those solutions I don't have an answer to why my program isn't writing to the text file I have assigned. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Main
{
    class Program
    {
        public static void Main (string[] args)
        {
            using (StreamWriter writer = new StreamWriter("test.txt"))
            {
                writer.WriteLine("Hello, World!");
            }
        }
    }
}

My program throws no exceptions and exits with 0, so I am not understanding how this is still not functioning properly. If someone could please provide an answer along with an explanation as to why this doesn't work, I would really appreciate it.

EDIT: Okay, I fixed the code after playing around a bit. Turns out that upon reading the file the text that I wrote is there. Thus, a clarification of this problem would be:

The text writes to the file, but it is not visible to me when I open up the file from my project in Visual Studio. I am not sure as to why, and this is leading to confusion

Upvotes: 0

Views: 147

Answers (1)

Gonzalo
Gonzalo

Reputation: 2876

Your sample is correct. The file is saved into build path. (where is "yourApp.exe"). You can try with a abosule path to define where file will be save, for example StreamWriter writer = new StreamWriter(@"c:\test\test.txt")

Upvotes: 2

Related Questions