1

Below is the code I have. My issue is that the file.txt is not created at all! I cannot find the reason. Program should create it. Could you please help me ?

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


namespace WriteToFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = "Please help me to write something!!";

          System.IO.StreamWriter file = new System.IO.StreamWriter
(@"C:\Users\jgonc\source\repos\WriteToFile\WriteToFile\bin\Debug\file.txt");

            file.Flush();
            file.WriteLine(line);
            file.Close();

            Console.WriteLine("press a key");
            Console.ReadKey();


        }
    }
}
Joooao
  • 13
  • 2
  • Does this answer your question? [Easiest way to read from and write to files](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) Please, check [msdn](https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-write-text-to-a-file) as well – Pavel Anikhouski Mar 01 '20 at 10:41
  • Do you have any exceptions (Access Denied)? If you don't, then you're not using that path when you're reading the file content back. You shouldn't hard-code a path anyway: it won't exist when you deploy your application. Note that the file content is overwritten using that constructor. -- Remove `file.Flush();` from there. – Jimi Mar 01 '20 at 11:21

3 Answers3

2
//line you want to write to the document
string line = "Please help me to write something!!";

//path to my documents
string docPath =
      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

using (StreamWriter sw = new StreamWriter(Path.Combine(docPath, "test.txt")))
{
    sw.WriteLine(line); 
}

Console.WriteLine("press a key");
Console.ReadKey();

Here I make use of the using keyword. This is extremely useful as any object created in the parameters will automatically be destroyed when the using segment ends which is great because it means you don't need to clutter your code with unnecessary flushes and closes.

Its also important you understand the difference between a flush & a close. Here when the using segment ends dispose is called on streamwriter which in turn calls close, which closes the stream. A flush is simply clearing the buffer.

  • Check that you the application has permission to write to my documents directory - here
  • I have changed the path to my documents as the bin folder often changes or is deleted.
James Mallon
  • 1,107
  • 9
  • 25
0

Thank you for your support and hints. I've investigate the permissions topic and it seems it was related to it. After uninstalling my antivirus it started to work.

below is the final code, to write a couple of strings using StreamWriter.

static void Main(string[] args)
        {
            string[] lines = {
                        "Please help me to write something!",
                        "It's working now, it was related with my antivirus, it was somehow blocking  my permissions to the folder",
                        "Thanks for your support and your hints!!",
            };

            //string path = @"C:\Users\jgonc\source\repos\WriteToFile\WriteToFile\bin\Debug\file.txt";
            string path = @"file.txt";

            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);  
            System.IO.StreamWriter file = new System.IO.StreamWriter(fs, System.Text.Encoding.ASCII);

            if (!File.Exists(path))
            {
                Console.WriteLine("file was not created");
            }
            else
            {

                using (file)
                {
                    foreach (string line in lines) {
                        file.WriteLine(line);
                    }
                }
            }
            fs.Close();

            Console.WriteLine("Press a Key to End...");
            Console.ReadKey();


        }
Joooao
  • 13
  • 2
-1

Try :

System.IO.StreamWriter file = new System.IO.StreamWriter
(@"C:\Users\jgonc\source\repos\WriteToFile\WriteToFile\bin\Debug\file.txt", true);

true for append attribute set it false if you want to create file every time.

Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29