简体   繁体   English

未授权访问异常 c#

[英]unauthorized access exception c#

I have this method我有这个方法

 public void Copy(string sourcePath, string destPath)
 {
     string[] files= Directory.GetFiles(sourcePath);
     for (int i = 0; i < files.Length; i++)
     {
         try
         {
             File.Copy(files[i], destPath);
         }
         catch
         {
             try
             {
                 File.Replace(files[i], destPath, null);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
    }
}

when I run it I get unauthorized access exception, Access denied !当我运行它时,我得到未经授权的访问异常,访问被拒绝! any help in this !任何帮助!

This exception is covered in the documentation for File.Copy: File.Copy文档中涵盖了此异常

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

Check the attributes of the file after the first copy. 第一次复制后检查文件的属性。 Are the permissions what you expect? 权限是您期望的吗? Do you need your program to run elevated (as administrator)? 您是否需要以管理员身份运行程序以提升权限?

below reasons can possible : 可能的原因如下:

The sourceFileName or destinationFileName parameter specifies a file that is read-only. sourceFileName或destinationFileName参数指定一个只读文件。

-or- This operation is not supported on the current platform. -或-当前平台不支持此操作。

-or- Source or destination parameters specify a directory instead of a file. -或-源或目标参数指定目录而不是文件。

-or- The caller does not have the required permission. -或者-呼叫者没有所需的权限。

Read link : http://msdn.microsoft.com/en-us/library/9etk7xw2(v=vs.110).aspx 阅读链接: http : //msdn.microsoft.com/zh-cn/library/9etk7xw2(v=vs.110).aspx

When the problem occurs on a Windows machine, make sure you have disabled "controlled folder access" in the App Windows-Safety or allow folder access to your program.当问题出现在 Windows 机器上时,请确保您已在 App Windows-Safety 中禁用“受控文件夹访问”或允许文件夹访问您的程序。 (must have administrator privileges) (必须有管理员权限)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM