Search code examples
c#streamwriter

I am unable to create new .txt file using StreamWriter Class


Here is my code...

string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Music", "stream.txt");

StreamWriter sw = new StreamWriter("stream.txt");
sw.WriteLine("i am stream");
sw.Close();

Solution

  • You almost had the solution there:

    string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),"Music", "stream.txt");
    
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine("i am stream");
                sw.Close();
    

    You just had to use the path variable you created :)

    After execution remember to look in the Music folder for the stream.txt file