[英]delete file throw exception UnauthorizedAccessException?
I am trying to delete file after I copy the file but I have this exception 复制文件后,我尝试删除文件,但是有此异常
System.UnauthorizedAccessException was unhandled
HResult=-2147024891
Message=Access to the path 'C:\Users\---\Downloads\file.zip' is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at CleanDownloadFolder.Program.CopyDirectory(String sourcePath, String targetPath) in c:\Users\Abdalla\Documents\Visual Studio 2012\Projects\CleanDownloadFolder\CleanDownloadFolder\Program.cs:line 48
at CleanDownloadFolder.Program.Main(String[] args) in c:\Users\Abdalla\Documents\Visual Studio 2012\Projects\CleanDownloadFolder\CleanDownloadFolder\Program.cs:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
C# code
if (Path.GetExtension(fileName) == ".zip")
{
System.IO.File.Copy(s, destFile, true);
System.IO.File.Delete(s);
}
If you have UAC
enabled and you are not running your application as an "Administrator"
you usually don't have access to delete from the system drive. 如果启用了
UAC
并且您不是以"Administrator"
身份运行应用程序,则通常无权从系统驱动器中删除。
have you tried copying the file to some other location ( like D:
) and deleting 您是否尝试过将文件复制到其他位置(
like D:
:)并删除
also you might find this interesting 你也可能会发现这个有趣
How does Read-Only affect a Directory when using C#/.Net? 使用C#/。Net时只读对目录有何影响?
The error is thrown because your application doesn't have the sufficient rights in order to delete the file at Downloads
. 引发错误是因为您的应用程序没有足够的权限才能在
Downloads
处删除文件。 You can either: 您可以:
System.Diagnostics.Process
namespace. System.Diagnostics.Process
命名空间在Administrator下生成您自己的新进程。 For more information, click here . AppData
directory. AppData
目录。 You can get the path of AppData using Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
, and then create a directory for your application. 您可以使用
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
获取AppData的路径,然后为您的应用程序创建目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.