简体   繁体   English

删除文件引发异常UnauthorizedAccessException吗?

[英]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: 您可以:

  1. Spawn a new process of your own under Administrator, using the System.Diagnostics.Process namespace. 使用System.Diagnostics.Process命名空间在Administrator下生成您自己的新进程。 For more information, click here . 有关更多信息, 请单击此处
  2. Download your file to another directory where you have the right to delete any file, such as the 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.

相关问题 在所有情况下,Windows File.Delete API都会引发UnauthorizedAccessException,尤其是在使用文件时? - In what all scenarios does Windows File.Delete API throw UnauthorizedAccessException, especially when a file is in use? 尝试写入文件时抛出UnauthorizedAccessException时,需求不引发异常 - Demand doesn't throw exception while the try to write to a file throws UnauthorizedAccessException 除非更改了文件扩展名,否则File.Delete上的UnauthorizedAccessException - UnauthorizedAccessException on File.Delete unless file extension is changed 为什么目录上的File.Delete()会产生UnauthorizedAccessException? - Why does File.Delete() on a directory yield UnauthorizedAccessException? C#无法删除文件-System.UnauthorizedAccessException - C# Cannot Delete File - System.UnauthorizedAccessException Directory.Delete - UnauthorizedAccessException - Directory.Delete - UnauthorizedAccessException 尝试删除文件,不删除或抛出错误 - trying to delete a file, doesnt delete or throw error UnauthorizedAccessException 试图删除文件夹中的文件,我可以在其中删除具有相同代码的其他文件 - UnauthorizedAccessException trying to delete a file in a folder where I can delete others files with the same code 从 System.IO.Abstractions 抛出 UnauthorizedAccessException - Throw UnauthorizedAccessException from System.IO.Abstractions 保存文件时出现UnauthorizedAccessException - UnauthorizedAccessException while saving a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM