DutchLearner
DutchLearner

Reputation: 335

Text file doesn't get saved but no errors (C#)

I've been looking on many websites now for the answer, but all working answers only work for the richTextbox, and I'm using the normal textbox. I'm trying to save the contents of the textbox to a file of choice, but for some reason the file doesn't get saved, and I have no idea what the problem is. This is the code of the 'save' menu item:

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {

        SaveFileDialog ofd = new SaveFileDialog();
        ofd.Title = "Save";
        ofd.Filter = "Txt Documents (.txt)|*.txt|All files (*.*)|*.*";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            try
            {
                //I don't know what to make of this, because clearly this doesn't work
                File.WriteAllText(@"./TestFile.txt", MainTextbox.Text);
            }
            catch (Exception ex)
            {
                MainTextbox.Text += ex;
            }
        }
    }

There is no error.

Upvotes: 2

Views: 2557

Answers (4)

DNR
DNR

Reputation: 3736

Use a try { } catch (Exception ex) { } block How to: Use the Try/Catch Block to Catch Exceptions

Upvotes: 0

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

I think its access denied issue.. try with 'D' drive ...

This is working example.. .WriteAllText works when file already exists and if file already exists then use AppendAllText

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}

Upvotes: 0

Tim S.
Tim S.

Reputation: 56536

You should be saving to the file selected in your SaveFileDialog, as retrieved by OpenFile(). This example worked for me:

SaveFileDialog ofd = new SaveFileDialog();
ofd.Title = "Save";
ofd.Filter = "Txt Documents (.txt)|*.txt|All files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
    using (var fileStream = ofd.OpenFile())
    using (var sw = new StreamWriter(fileStream))
        sw.WriteLine("Some text");
}

In your code, you let the user select a file to save to, then ignore that and write it to a hardcoded location. It's possible your app didn't have permissions to do this, but it should have permissions to write to a location the user selected.

Upvotes: 5

MgSam
MgSam

Reputation: 12803

First off, saving the file has nothing to do with where the text is coming from, rich text box or normal text box.

As Brian S. said in a comment, it is likely there is an exception because you're writing to the C drive. You should use a relative path: "./MyTest.txt"

Upvotes: 0

Related Questions