Skip to main content
 首页 » 编程设计

c#之System.IO.File.Copy 抛出 System.UnauthorizedAccessException

2024年02月13日15jyk

仅当文件已存在于目标位置时,我才尝试将文件从一个位置复制到另一个位置时出现错误。尽管将覆盖标志设置为 true,也会发生这种情况。

我没有得到任何线索。 这是代码。

    System.IO.File.Copy(source, destination, true);// goes fine as the destination file   is not present 
       System.IO.File.Copy(source, destination, true);// Throws the exception    
                                                     //"Access to the path 'C:\\Program  Files (x86)\\testapp\\map\\sometext.txt' is denied." 

请指导。

谢谢 苏尼尔

请您参考如下方法:

此异常包含在 documentation for File.Copy 中:

The caller does not have the required permission.
-or-
destFileName is read-only.

第一次复制后检查文件的属性。权限是您期望的吗?您是否需要您的程序以提升的方式运行(以管理员身份)?