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!

Recommended Answers

All 5 Replies

add

file.close();

doesnt work.

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

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.

Try this:

void button1_Click(object sender, EventArgs e)
    {
      string s = Console.ReadLine();
      System.IO.File.WriteAllText(@"C:\path\to\file.txt", s);
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.