2

I want to save my Files in a more generic way than on Desktop. So i want to create my own Subfolder in Programs Folder, which i can use to save my stuff. But i get "System.UnauthorizedAccessException" if i try to create a File using File.AppendAllText(@"C:\Program Files\MySubfolder\MyFile.txt,someString);

I even disabled the Protection of the Folders manually. My App is not yet compiled so i cant run it as administrator, can i? How does every Program use this Folder but i cant? Do i need to compile my App everytime i make a small change and want to test it?

I would really apreciate Help since im stuck with that multiple hours now

  • 2
    The proper place to save your own documents is the Document folder. – Steve Mar 01 '21 at 21:53
  • 1
    `How does every Program use this Folder but i cant?` No well-behaved program will write there, instead, they'll use AppData or Documents or anything within the user profile. – Alejandro Mar 01 '21 at 21:54
  • 2
    AppData is another common place.https://stackoverflow.com/a/64756445/920069 – Retired Ninja Mar 01 '21 at 21:55

1 Answers1

3

It is a very bad practice to try to write in Program Files. This folder as well as other sensitive folders are protected by the OS to prevent malicious code hide between your programs or to prevent unsavy users from messing on the installed programs.

If you want to write your private stuff on your disk you can use these folders

string folder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string myFolder = Path.Combine(folder, "MyReservedPath");
Directory.CreateDirectory(myFolder);  // if exists does nothing
string myFile = Path.Combine(myFolder, "MyPrivateData.txt");
File.WriteAllText(myFile, dataToWriteOnDisk);

The CommonApplicationData resolves to C:\programdata and this place is usually used to store information needed by your program for any user that uses it.

If you want to store some data that your program produces then it is better to use the

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

There are many other places available, just look at the Environment.SpecialFolder enum.

This code will give you a list of everything mapped to the actual folders in your system

foreach (Environment.SpecialFolder x in Enum.GetValues(typeof(Environment.SpecialFolder)))
    Console.WriteLine($"{x} = {Environment.GetFolderPath(x)}");
Steve
  • 213,761
  • 22
  • 232
  • 286
  • This looks promising, i'll definitely try this tomorow. but if using Program Files folder is so bad, why does so much major Software do it? – Leon Petzold Mar 01 '21 at 22:13
  • 1
    Program Files is where you install programs, they usually do not try to write there. If they need some supporting file (dynamic configurations, user preferences etc, they write into programdata or appdata inside the userprofile. If you look there you will find a lot of stuff written by the programs that you have installed – Steve Mar 01 '21 at 22:15
  • 1
    When the user installs a program is well aware of the fact and can give the permission to write the intial stuff there. Now imagine if some virus writer would be able to replace some well know program with a virus. This happened in the past and this is one of the reasons that they now make very difficult to write there – Steve Mar 01 '21 at 22:17