I'm getting a Unauthorized Access Exception
If it helps, this is the code where the exception ocurrs:
protected void DeleteImage(string imageName) { if (imageName != null) { string f = String.Format("~/Images/{0}", imageName); f = System.Web.Hosting.HostingEnvironment.MapPath(f); if (File.Exists(f)) { if (f != null) File.Delete(f); } } }
Why could this happen?
Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.
File deleteFile = new File(s. getFilepath()); boolean delete = deleteFile. delete();
I encountered the same problem, and found that writing my own Directory.Delete wrapper fixed it up. This is recursive by default:
using System.IO; public void DeleteDirectory(string targetDir) { File.SetAttributes(targetDir, FileAttributes.Normal); string[] files = Directory.GetFiles(targetDir); string[] dirs = Directory.GetDirectories(targetDir); foreach (string file in files) { File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } foreach (string dir in dirs) { DeleteDirectory(dir); } Directory.Delete(targetDir, false); }
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