4

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);
Frode Stenstrøm
  • 1,048
  • 7
  • 24
user296623
  • 325
  • 3
  • 7
  • 18
  • Can you post the exact line that throw the exception ? Seems like the error is your way to get the pathname – LoSciamano Jun 07 '11 at 16:16
  • Are you sure you have not got any handles (files) still open? – Polyfun Jun 07 '11 at 16:19
  • 4
    It's called **[UAC](http://stackoverflow.com/questions/5210575/does-windows-7-have-the-same-problem-as-vista/5210642#5210642)**. You aren't supposed to be writing to that directory at all. Luckily, there are alternatives. – Cody Gray - on strike Jun 07 '11 at 16:23
  • Thanks for the response. I have windows xp. Still I tried the above appdata folder. Its still the same. The path now is C:\Documents and settings\UserName\Application Data\672011115302AM – user296623 Jun 07 '11 at 16:49
  • LoSciamano: Here is the code where its throwing error: File.Delete(p_folderpath); where p_folderpath is C:\Documents and Settings\UserName\Application Data\672011115302AM – user296623 Jun 07 '11 at 16:51
  • this sounds weird but XP has a weak user access controll. Maybe you can use Temp directory as [this reply](http://stackoverflow.com/questions/6268326/access-to-the-path-is-denied/6268604#6268604) suggest – LoSciamano Jun 07 '11 at 17:02

3 Answers3

8

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).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks for the response. I have windows xp. Still I tried the above appdata folder. Its still the same. The path now is C:\Documents and settings\UserName\Application Data\672011115302AM – user296623 Jun 07 '11 at 16:49
  • @user296623: Yes, and in XP, it's preferred to do that too. In Vista+, though, you'll have even more issues... – Reed Copsey Jun 07 '11 at 16:51
  • @user296623: If you can't delete the folder, it probably means something is holding a handle to a file open inside of it... – Reed Copsey Jun 07 '11 at 16:51
2

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);
Riaan
  • 3,423
  • 4
  • 30
  • 36
1

or you can use Path.GetTempPath() to get path to temp directory and create your temp data in there.

good thing about using Temp is that if you got forget/fail to clean it up, when drive space is slow, system can help you to clean it up.

Bek Raupov
  • 3,782
  • 3
  • 24
  • 42