I am having a problem where I am trying to delete my file but I get an exception.
if (result == \"Success\")
{
if (FileUpload.HasFile)
{
t
First just check the path if the colon(:) character is missing or not after the drive letter. If colon is not missing then you can check if access/write permission is granted for that path. I had the same issue and i was only missing the colon, permission and everything else was fine.
C:\folderpath
will work fine but,
C\folderpath .........(missing colon)
will give you access denial error.
To solve this problem, I follow the Scot Hanselman approach at Debugging System.UnauthorizedAccessException (often followed by: Access to the path is denied) article, the code with example is bellow:
class Program
{
static void Main(string[] args)
{
var path = "c:\\temp\\notfound.txt";
try
{
File.Delete(path);
}
catch (UnauthorizedAccessException)
{
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(path, attributes);
File.Delete(path);
}
else
{
throw;
}
}
}
}
I was facing this error because
Sometimes when I Combine
the path with File Name and FileName = ""
It become Path Directory
not a file
which is a problem as mentioned above
so you must check for FileName
like this
if(itemUri!="")
File.Delete(Path.Combine(RemoteDirectoryPath, itemUri));
I got the error because I didn't realize that the destination should be a file. I had a folder as the second parameter (which works in cmd). and I got Unhandled Exception: System.UnauthorizedAccessException: Access to the path is denied.
because C# File.Move
wants a file there, not just for the first parameter, but for the second too, and so if you put a directory as second parameter, it's trying to write a file like c:\crp
when you have a directory called c:\crp
.
this would be incorrect File.Move(args[0],"c:\\crp");
So, this would be correct File.Move(args[0],"c:\\crp\\a.a");
The same goes for File.Copy
I had this error thrown when I tried to rename a folder very rapidly after it had been either moved or created.
A simple System.Threading.Thread.Sleep(500);
solved it:
void RenameFile(string from, string to)
{
try
{
System.IO.File.Move(from, to)
}
catch
{
System.Threading.Thread.Sleep(500);
RenameFile(from, to);
}
}
Right click on Visual studio and click Run as Administrator