0

Okay so i wanted to know how i would take something that is in a textBox, then i press a button, the contents of the textBox will be saved to a file location, then when i load the .exe back up, the contents will reappear in the textBox.

This is what i have so far

private void button3_Click(object sender, EventArgs e)
{
File.WriteAllText(@"C:\Application.txt", textBox1.Text);
}

^To Write it to a file location, I have tried this multiple times but it doesnt seem to want to make the file on my C:.

private void Form1_Load(object sender, EventArgs e)
{
try
{
textBox1.Text = File.ReadAllText(@"C:\Application.txt", Encoding.ASCII);
}
catch
{

}

^To Load the file then inject it back into the textbox it came from

Any and all help is appreciated, Thanks.

4
  • 1
    The code itself seems correct to me. Are you sure you don't get a permission error or something when saving the file? Apr 16, 2014 at 1:34
  • 1
    And? Code looks reasonably (short of possible "access denied" to write on root of c: ) - Did you get any errors/exceptions? Apr 16, 2014 at 1:35
  • no errors pop up so unless its hidden i would say no
    – Wire
    Apr 16, 2014 at 1:36
  • @Dakota do you have a try-catch that could suppress the error? I would be quite surprised if the error is not as described in my answer below. Apr 16, 2014 at 1:37

3 Answers 3

3

You likely get an exception when trying to write to your C drive because it requires administrative access. Try running Visual Studio as Administrator (therefor the app will run as admin when kicked off from VS) or try writing to another location. Your code is all fine. The Encoding.ASCII bit is unnecessary though and I recommend removing it (more than likely that's not the encoding you will write the file in).

2
  • I ran as administrator and the file wasn't created, so any suggestions where i could create the file?
    – Wire
    Apr 16, 2014 at 1:39
  • 1
    @Dakota you could use a relative path like @".\Application.txt", that will write to the folder where your binaries are dropped. Also, you could give it a path to something like C:\Users\Dakota\Documents or whatever. Apr 16, 2014 at 1:43
1

Trying to write directly to the C: drive can cause problems.

Try writing to a location that you definitely have write access to. You could use the ApplicationData directory (for application files unique to the current user), or use SpecialFolder.MyDocuments if you prefer.

private string applicationFilePath = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData), "Application.txt");

private void button3_Click(object sender, EventArgs e)
{
    File.WriteAllText(applicationFilePath, textBox1.Text);
}

private void Form1_Load(object sender, EventArgs e)
{
    textBox1.Text = File.ReadAllText(applicationFilePath, Encoding.ASCII);
}
0

I would do something like this:

using System.IO;

private void button3_Click(object sender, EventArgs e)
{
    using (var stream = new FileStream(@"C:\Application.txt", FileMode.Create))
    using (var writer = new StreamWriter(stream, Encoding.ASCII))
        writer.Write(textBox1.Text);
}

private void Form1_Load(object sender, EventArgs e)
{
    using (var stream = new FileStream(@"C:\Application.txt", FileMode.Open))
    using (var reader = new StreamReader(stream, Encoding.ASCII))
        textBox1.Text = reader.ReadToEnd();
}

I think this method gives you more control on your content. Try to explore the contents of FileMode enum, and make sure to add your using System.IO; directive.
Be careful not to confuse the using statement with the using directive.

Also, always remember to dispose/close your stream when done to ensure that the data has been flushed and that the file is no longer in use by your application. Here, the using statement does the job of disposing when the stream is no longer in use.

EDIT: As mentioned by the other posts, writing to the C: directory causes problems on newer operating systems due to Admininstrative Access restrictions. Make sure to write to different drives/folders that you definitely have access to.

// Current User
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Application.txt");
// All users
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Application.txt");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.