C# 拒绝访问路径“c:\ApplicationFolder”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/364336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:39:58  来源:igfitidea点击:

Access to the path 'c:\ApplicationFolder' is denied

c#.netwinforms

提问by

I am getting a strange error on a remote windows clients (WinForm application using C# 2.0)

我在远程 Windows 客户端上遇到一个奇怪的错误(使用 C# 2.0 的 WinForm 应用程序)

Error Message: Access to the path 'c:\ApplicationFolder' is denied.

错误消息:拒绝访问路径“c:\ApplicationFolder”。

Stack Trace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

堆栈跟踪:在 System.IO.__Error.WinIOError(Int32 errorCode, String MaybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options , SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

Let me say I know I should not put the application folder directly off the c:\ folder. This an old application that I have no control over.

让我说我知道我不应该将应用程序文件夹直接放在 c:\ 文件夹之外。这是我无法控制的旧应用程序。

回答by Joel Coehoorn

It likely comes down to the reasonthat you shouldn't put the application folder there: standard users don't have write access by default. Since most people run Windows as administrator it's not often a problem, but if you try to install on a corporate environment that recently updated security policies you would suddenly have a serious problem.

这可能归结为理由,你不应该把应用程序文件夹有:标准用户不具有默认的写入权限。由于大多数人以管理员身份运行 Windows,这通常不是问题,但如果您尝试在最近更新了安全策略的公司环境中安装,您会突然遇到严重问题。

Have you checked the permissions on the folder?

你检查过文件夹的权限吗?

回答by Austin Salonen

Does the app at least load the "application folder" from a config file / the registry / a database table? If so, what happens when you change that value to something in the My Documents folder?

应用程序是否至少从配置文件/注册表/数据库表加载“应用程序文件夹”?如果是这样,当您将该值更改为“我的文档”文件夹中的某些内容时会发生什么?

I saw you added the user is an admin and has full admin rights so I have to ask the obvious -- does the folder exist where the code thinks it should?

我看到您添加了用户是管理员并且具有完全管理员权限,所以我不得不问一个显而易见的问题 - 该文件夹是否存在于代码认为应该存在的位置?

回答by Bevan

A question, and a suggestion.

一个问题,一个建议。

Does the file path reported in the message exactlymatch the name of the folder - letter casing, spaces, underscores, accents, everything?

消息中报告的文件路径是否与文件夹的名称完全匹配 - 字母大小写、空格、下划线、重音符号等等?

Why? I've seen oddball cases where this caused a problem.

为什么?我见过一些奇怪的案例,这导致了问题。

Suggestion: Use ProcessMonitor(from SysInternals, now part of Microsoft) to watch access to the folder, you'll see more details about the error - especially useful if the error reporting you're seeing isn't accurate.

建议:使用ProcessMonitor(来自 SysInternals,现在是 Microsoft 的一部分)查看对文件夹的访问,您将看到有关错误的更多详细信息 - 如果您看到的错误报告不准确,则特别有用。

回答by Bevan

Agree with process monitor being helpful, saved several hours of my day. Had the args to Path.Combine flipped so I ended up trying to write to the directory in lieu of the file. no hints in debug, process mon showed right up.

同意过程监视器很有帮助,节省了我一天的几个小时。如果 Path.Combine 的 args 翻转,所以我最终尝试写入目录而不是文件。调试中没有提示,进程 mon 就出现了。

回答by Dour High Arch

System.IO.FileStream.Initsounds like you're opening a file, not a folder. What is the file?

System.IO.FileStream.Init听起来你正在打开一个文件,而不是一个文件夹。文件是什么?

Are you sure the file exists? That it is not opened by another app? Are you trying to write to the file? Is it locked or read-only?

你确定文件存在?它没有被其他应用程序打开?您是否正在尝试写入文件?它是锁定的还是只读的?

We need to see the code that's opening the file.

我们需要查看打开文件的代码。

回答by Dour High Arch

You are probably trying to get write access on read-only file.

您可能正在尝试获得对只读文件的写访问权限。

回答by techno

Create and Embed Manifest File in Your Application to get the Administrative Rights for your application

在您的应用程序中创建并嵌入清单文件以获得应用程序的管理权限

Executable: IsUserAdmin.exe 
Manifest:IsUserAdmin.exe.manifest
Sample application manifest file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
  <assemblyIdentity version="1.0.0.0"
     processorArchitecture="X86"
     name="IsUserAdmin"
     type="win32"/> 
  <description>Description of your application</description> 
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>

Here requireAdministrator says your application requires admin rights This will cause a confirmation Dialogue when user starts your app.

这里 requireAdministrator 说您的应用程序需要管理员权限这将在用户启动您的应用程序时导致确认对话。