0

Hello, I have a problem with one basic method - saving a text file. I have no idea why it doesn't work:

static void Main()
        {
            Console.Write("Write the statement:");
            string statement = Console.ReadLine();

            Console.Write("Write the name of the file:");
            string file_name = Console.ReadLine();

            StreamWriter file = new StreamWriter("c:\\" + file_name + ".txt");

            file.WriteLine(statement);
        }

Thanks for any help!

4
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by sknake
0

if you are using windows vista or seven it could be privilege issue try to save another path or run as administrator

0

In either case, I would put the file operations in a try/catch block.
the exception.Message will tell you the error.

It is probably that youur account does not have permission to write in the root of C.

Edited by thines01: n/a

0

Try this:

void button1_Click(object sender, EventArgs e)
    {
      string s = Console.ReadLine();
      System.IO.File.WriteAllText(@"C:\path\to\file.txt", s);
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.