2

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.

DutchLearner
  • 335
  • 3
  • 13
  • 7
    Shouldn't you be saving to the file selected in your `SaveFileDialog`? – Tim S. Jul 02 '12 at 19:03
  • @TimS. Certainly does defeat the purpose, the way it's currently set up. I'd call your suggestion an answer. – erodewald Jul 02 '12 at 19:05
  • 3
    Are you sure there's no exception? Perhaps it is being swallowed somewhere up the chain? You're trying to create a file on the root of your C:\ drive, so it is very possible (and likely, unless you've taken other steps) that you don't have permission to create a file there. My assumption is that you're using the hard-coded file name to test out if you can create a file, otherwise, the comment from Tim S above may be the solution. – Brian S Jul 02 '12 at 19:06
  • Try with 'D' drive path.. may be you are facing access denied issue because of 'C' drive.. – Dr. Rajesh Rolen Jul 02 '12 at 19:17

4 Answers4

5

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.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

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

Fabio
  • 3,020
  • 4
  • 39
  • 62
DNR
  • 3,706
  • 14
  • 56
  • 91
0

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);
    }
}
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
0

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"

MgSam
  • 12,139
  • 19
  • 64
  • 95