System.UnauthorizedAccessException:访问路径“ ...”。被拒绝 [英] System.UnauthorizedAccessException: Access to the path "..." is denied

查看:924
本文介绍了System.UnauthorizedAccessException:访问路径“ ...”。被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用单击一次安装使用.net完成了C#wpf安装。一切正常。然后,我有以下代码是已安装程序的一部分:

I have C# wpf installation done with .net using click once installation. All works fine. Then I have the following code which is part of the installed program:

String destinationPath = System.Windows.Forms.Application.StartupPath + "\\" + fileName;
File.Copy(path, destinationPath, true);
this.DialogResult = true;
this.Close();

但我收到此错误:


System.UnauthorizedAccessException:拒绝访问路径C:\user\pc\appdata\local\apps\2.0.......。

System.UnauthorizedAccessException: Access to the path C:\user\pc\appdata\local\apps\2.0....... is denied.

(String sourceFileName,String destFileName,Boolean overwrite,Boolean checkHost)
在System.IO.File.Copy(String sourceFileName,String destFileName,布尔覆盖)

at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)

是权限错误还是我需要对代码中的内容进行调整?

Is it a permission error or do I need to tweak something in my code?

让我感到困惑的是,为什么用户只需单击一次即可将程序安装到该目录中而没有任何问题,但是将文件上传到该目录不起作用?

What puzzles me is why the user is able to install the program using click once into that directory without any issues, but uploading a file to it doesn't work?

推荐答案

在安装应用程序时,安装程​​序通常会要求管理权限。如果用户选择是,则该程序将运行,并且具有比普通用户更大的读取和写入访问权限。如果这种情况导致安装程序不要求管理特权,则可能只是ClickOnce自动以某种提升的特权运行。

When installing an application the installer usually asks for administrative privileges. If the user chooses "Yes" the program will run and have read and write access to a larger variety of paths than what a normal user has. If the case is such that the installer did not ask for administrative privileges, it might just be that ClickOnce automatically runs under some sort of elevated privileges.

我建议您而是写到本地appdata文件夹,但是如果您真的想写与应用程序相同的目录,则必须首先以管理员权限运行应用程序。

I'd suggest you write to the local appdata folder instead, but if you feel you really want to write to the very same directory as your application you must first run your app with administrator privileges.

要使您的应用程序始终要求管理员特权,您可以修改应用程序的清单文件,并将 requestedExecutionLevel 标记的 level 属性设置为 requireAdministrator

To make your application always ask for administrator privileges you can modify your app's manifest file and set the requestedExecutionLevel tag's level attribute to requireAdministrator:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

您可以在 如何强制.NET应用程序以管理员身份运行?

这篇关于System.UnauthorizedAccessException:访问路径“ ...”。被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆