-1

it says that the path to the file is denied. looked for an hour no real answers. please help.

private void btnSetText_Click(object sender, EventArgs e)
{
    using (OpenFileDialog dlg = new OpenFileDialog())
    {
        dlg.InitialDirectory = Application.StartupPath;
        dlg.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*"; //https://stackoverflow.com/questions/48151581/system-argumentexception-filter-string-not-valid
        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            txtSetText.Text = dlg.FileName;
        }
    }

    System.IO.File.WriteAllText(txtSetText.Text , text);

every thing is fine and valid, but at the line:

System.IO.File.WriteAllText(txtSetText.Text , text);

I keep getting access to path xyz is denied. How do I make it accessable?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
osenter
  • 1
  • 2
  • Can you access using a file explorer? Are you prompted to use admin rights? In this case, you must use elevated privileges: [Request Windows Vista UAC elevation if path is protected](https://stackoverflow.com/questions/17533/request-windows-vista-uac-elevation-if-path-is-protected) & [Getting Elevated Privileges on Demand using C# (Codeproject)](https://www.codeproject.com/Articles/105506/Getting-Elevated-Privileges-on-Demand-using-C). Else if you don't have rights on the file, you need to check/change [permissions](https://stackoverflow.com/questions/7590446/set-file-permissions-in-c-sharp). –  Dec 12 '20 at 13:56
  • So if the file is locked by another process, can you confirm the case to possibly flag the question as duplicated, please? –  Dec 12 '20 at 14:03

1 Answers1

-1

You need to ensure that your destination file is not opened by another program and you need to be sure that your program user have the rights to open and edit this file.

Gambi
  • 464
  • 4
  • 12