Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Files/Folders to Program Files x86 Folder with C#

I am trying to create a file directory (folder) in the 32 bit Program Files folder to store data that a user will be creating in the program. However, it keeps saying that access is denied when I try to do so. Is there anyway to give the program permission to access the 32 bit program files folder and add subsequent folder and files to it? Here is my code below which is producing a run-time error due to not having permission.

string mainDirectory=Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)+"\\UKC Trial Reporter Files";
if (!Directory.Exists(mainDirectory))
{
    Directory.CreateDirectory(mainDirectory);
}
like image 437
user1546315 Avatar asked Dec 19 '25 17:12

user1546315


1 Answers

Store your data in Environment.SpecialFolder.ApplicationData, Environment.SpecialFolder.LocalApplicationData or Environment.SpecialFolder.CommonApplicationData (if data have to be accessible by any user).

Never store it in %ProgramFiles%. %ProgramFiles% is intended for... program files: executables, dlls, app.config files.

In addition of security reasons, there's yet another reason to do this never: when uninstalling, your application's installer must sweep all of files and folders it had created. And this is impossible, when list of files in installdir was changed during application lifetime.

Fortunately, we have UAC since Windows Vista has released.

like image 152
Dennis Avatar answered Dec 21 '25 10:12

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!