Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to the path is denied [duplicate]

Tags:

c#

wpf

For my WPF application, I have to create folders with images files for eg: C:\Pearl\Src\TEMP. Later when those files are not needed, I am deleting the folders programmatically. But I get "Access to the path ' ' is denied". I also tried to assign access rights to the temporary folders created but not of much use.

using System.IO;

var activeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var dInfo = Directory.GetParent(Path.GetDirectoryName(activeDir);

var dSecurity = dInfo.GetAccessControl();

dSecurity.AddAccessRule(new FileSystemAccessRule(@"ATSDEV\ABCD", FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow));

dInfo.SetAccessControl(dSecurity); // Set the new access settings.

var ImageDir = Path.Combine(dInfo.ToString(), "TEMP");

System.IO.Directory.CreateDirectory(ImageDir, dSecurity);
like image 834
user296623 Avatar asked Dec 21 '22 13:12

user296623


2 Answers

In Vista+, you shouldn't ever write to the installation folders or parent folders of the executing process. Instead, you should consider writing to a subdirectory in the User's AppData folder, as this will be more appropriate, and not cause permission issues.

You can get the appropriate folder via Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).

like image 98
Reed Copsey Avatar answered Jan 06 '23 19:01

Reed Copsey


I also had the problem, hence me stumbling on this post. I added the following line of code before and after a Copy / Delete.

Delete

File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);

Copy

File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);
like image 25
Riaan Avatar answered Jan 06 '23 17:01

Riaan