C# 访问文件被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006140/
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-06 05:20:53  来源:igfitidea点击:

File Access Denied

c#file-iopermissions

提问by

I am using an FTPClient library to transfer files from a Windows share to an FTP server.

我正在使用 FTPClient 库将文件从 Windows 共享传输到 FTP 服务器。

The SendFile method of the library uses the following code:

库的 SendFile 方法使用以下代码:

FileStream stream = new FileStream(localFileName, FileMode.Open);

This results in a System.UnauthorizedAccessException being thrown, however I am able to open, rename, and move the file using Windows Explorer under the same user account which the code is being executed.

这会导致抛出 System.UnauthorizedAccessException,但是我可以使用 Windows 资源管理器在执行代码的同一用户帐户下打开、重命名和移动文件。

Can anyone tell me why this is happening?

谁能告诉我为什么会这样?

Edit:

编辑:

The strange thing is that I can access other files on the share which have been granted the same NTFS permissions as the one that I can't.

奇怪的是,我可以访问共享上的其他文件,这些文件已被授予与我不能访问的文件相同的 NTFS 权限。

This is also a Windows forms app.

这也是一个 Windows 窗体应用程序。

Update:

更新:

Still no luck with this. I am able to read the file using a StreamReaderbut not a file stream. I can't understand why the two behave differently.

仍然没有运气。我可以使用StreamReader文件流读取文件,但不能使用文件流。我不明白为什么两者的行为不同。

回答by abmv

1) NTFS permissions on the physical directory using explorer

1) 使用资源管理器对物理目录的 NTFS 权限

2) Within the IIS MMC console FTP Site to allow read/write on the FTP folder

2) 在 IIS MMC 控制台 FTP 站点内允许对 FTP 文件夹进行读/写

3) Ensure that the FTP Site or virtual directory actually exists, when checking the above step

3) 检查上述步骤时,确保FTP站点或虚拟目录确实存在

http://www.eggheadcafe.com/forumarchives/inetserveriisftp/Jan2006/post25322215.asp

http://www.eggheadcafe.com/forumarchives/inetserveriisftp/Jan2006/post25322215.asp

回答by simon831

The process that is running your code does not have permissions on the file. Is it part of a web application - if so you need to give access to the ASPNET account.

运行您的代码的进程对该文件没有权限。它是 Web 应用程序的一部分 - 如果是,您需要授予对 ASPNET 帐户的访问权限。

Give permission to 'everyone' on the file, and see if it still has problems.

向文件上的“所有人”授予权限,看看它是否仍然有问题。

回答by cjk

Is your project being run from a network drive? If so that that will mean it runs in a restricuted priviliges mode that could cause this. Try copying the project to your C drive and running it again.

您的项目是从网络驱动器运行的吗?如果是这样,那将意味着它以可能导致这种情况的受限特权模式运行。尝试将项目复制到 C 驱动器并再次运行它。

回答by fretje

Are you sure it's the same user account? Can you try something like

你确定这是同一个用户帐户?你能试试

MessageBox.Show(WindowsIdentity.GetCurrent().Name);

?

?

Also, are you sure the file isn't read-only? Do you need write access to the file? Otherwise you could try:

另外,您确定该文件不是只读的吗?您需要对文件进行写访问吗?否则你可以尝试:

FileStream stream = new FileStream(localFileName, FileMode.Open, FileAccess.Read);

回答by Chernikov

It's near FileSecurity class.

它靠近 FileSecurity 类。

See at FileSecurity class

参见FileSecurity 类

and try:

并尝试:

        // Get a FileSecurity object that represents the
        // current security settings.
        FileSecurity fSecurity = File.GetAccessControl(localFileName);

        // Add the FileSystemAccessRule to the security settings.
        fSecurity.AddAccessRule(new FileSystemAccessRule("DOMAIN\USERNAME",
            FileSystemRights.ReadData, AccessControlType.Allow));

        // Set the new access settings.
        File.SetAccessControl(localFileName, fSecurity);