0

I'm encountering an error in creating a textfile to a specific folder. Specifically its path. It says :

UnauthorizedAccessException was unhandled

Access to path 'E:\Majel\Tic Tac Toe\TextFiles' is denied.

I tried a lot of ways to fix this, some others said, It's the permission that you can't access. But my PC is running as an administrator and still Im encountering the said problem.

Here's my code:

private void Form1_Load(object sender, EventArgs e)
{
    string fileName = textBox1.Text;    
    File.WriteAllText("E:\\Majel\\Tic Tac Toe\\TextFiles" + fileName, "\nYou are Logged In!");
}

Can someone help me to fix this?

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67

1 Answers1

0

You really should do some validation before you attempt to write to a file. If your positive you have access to the directory, try adding the extension to the end of the file name. A question with some validation can be found here.

You should write your path like this:

string path = Path.Combine(@"E:\Public", filename);

Hopefully this helps, as I mentioned you should validate your directory before writing.

You could use this also:

using(StreamWriter writer = File.Create(path))
     writer.Write(content);
Community
  • 1
  • 1
Greg
  • 11,302
  • 2
  • 48
  • 79
  • sir, but Im trying to name the textfile from the contents of textbox. Example: in textbox, I input **file** then when I click the button, the content of textbox will be automatically the name of textfile. So, **file.txt** – user3349418 Sep 27 '14 at 04:00
  • That is under the assumption that your user inputs the extension. – Greg Sep 27 '14 at 04:47
  • sir, can you provide example? – user3349418 Sep 27 '14 at 04:53