-1

I'm trying to code an app that save parameters in C:\Program(x86). But I'm not allowed to write/read on this folder that is why I have an error message

My app works perfectly when I change the installation folder (not in C:). If i do understand it is linked to my access rights.

DirectoryInfo dir_mainfold;
private void main_fold()
        {

            try
            {

                if (File.Exists(".\\Configuration\\main_fold.txt"))
                {

                    string[] sConf = File.ReadAllLines(".\\Configuration\\main_fold.txt");

                    dir_mainfold = new DirectoryInfo(sConf[0]);
                    textBox7.Text = dir_mainfold.ToString();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message + " " + ex.StackTrace);
            }

        }

I want it to delete the file if it exists, and create it/ write the content of my textbox on it if not.

Rhon Yz
  • 145
  • 1
  • 1
  • 8

2 Answers2

9

If you want to access the program files folder, you need to run your program with elevated rights/as administrator.

However, it's best practice to store your application data in the AppData-folder and not in the program directory. See this post for more details.

Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
1

You either have to run the app as admin or save your parameters inside another folder

Marce
  • 467
  • 1
  • 6
  • 10