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);
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).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With