简体   繁体   English

UnauthorizedAccessException:Winform拒绝访问路径

[英]UnauthorizedAccessException: Access to the path is denied winform

I'm using FtpWebRequest to download my file from ftp. 我正在使用FtpWebRequest从ftp下载我的文件。 However, when the app goes to the line FileStream which I declare to write the file out. 但是,当应用程序转到FileStream行时,我声明将其写出。

Below is my Download function: 以下是我的下载功能:

public void Download(List <string> path)
        {
            try
            {
                string timenow = DateTime.Today.Year.ToString() + "_" + DateTime.Today.Month.ToString() + "_" + DateTime.Today.Day.ToString() + "_" + DateTime.Today.Hour.ToString() + "_" + DateTime.Today.Minute.ToString() + "_" + DateTime.Today.Second.ToString();
                DirectoryInfo dir = new DirectoryInfo(@"C:\StudySystemFile\" + timenow);
                if (!dir.Exists)
                    dir.Create();

                foreach (string p in path)
                {
                    FtpWebRequest request;
                    request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp.Hostname + p));
                    request.Credentials = new System.Net.NetworkCredential(ftp.FtpAccount, ftp.FtpPassword);
                    request.Method = WebRequestMethods.Ftp.DownloadFile;
                    request.KeepAlive = false;
                    request.UseBinary = true;
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    Stream reader = response.GetResponseStream();
                    FileStream file = new FileStream(@"C:\StudySystemFile\" + timenow, FileMode.Create, FileAccess.ReadWrite);

                    byte[] buffer = new byte[1024];
                    int bytesRead = reader.Read(buffer, 0, buffer.Length);

                    while (bytesRead > 0)
                    {
                        file.Write(buffer, 0, bytesRead);
                        bytesRead = reader.Read(buffer, 0, buffer.Length);
                    }

                    reader.Close();
                    file.Close();
                    response.Close();
                    Console.WriteLine(p);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

The user running the program does not have permission to create the file at C:\\StudySystemFile\\ check that the folder exists and whatever user the program is running as can create files there without a UAC prompt. 运行该程序的用户无权在C:\\StudySystemFile\\创建文件,请检查该文件夹是否存在,并且正在运行该程序的任何用户都可以在没有UAC提示的情况下在此处创建文件。

Likely what is happening is when you create the folder with Create() it is inheriting permissions from C:\\ which does not have write permissions for files, you only get folder creation permissions. 可能发生的情况是,当您使用Create()创建文件夹时,它正在从C:\\继承权限,而C:\\没有文件的写权限,您只会获得文件夹创建权限。

You either need to create the folder outside the program or when you call Create() you need to set the permissions . 您需要在程序外部创建文件夹,或者在调用Create()时需要设置权限

You must be getting UnauthorizedAccessException .The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error. 您必须获取UnauthorizedAccessException 。当操作系统由于I / O错误或特定类型的安全错误而拒绝访问时引发的异常。

It is clearly a permission issue . 显然,这是一个许可问题

In case of Vista/Windows 7/8, C:\\ drive is considered as system drive, and need administrator privileges for your process in order to create files directly under it. 对于Vista / Windows 7/8, C:\\驱动器被视为系统驱动器,需要您的进程的管理员特权才能直接在其下创建文件。

Try running your process with administrator or Run Visual Studio as Administrator , and it should work. 尝试以管理员Run Visual Studio as Administrator运行您的进程或Run Visual Studio as Administrator ,它应该可以工作。

Your application doesn't have permission to write to that directory. 您的应用程序无权写入该目录。 You can force the UAC prompt by right-clicking on your project and adding a new Application Manifest File . 您可以通过右键单击您的项目并添加一个新的Application Manifest File来强制UAC提示。 In this file, replace 在此文件中,替换

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

with

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

A better idea would be to write to a directory that doesn't require admin rights. 一个更好的主意是写到不需要管理员权限的目录。

What version of Framework are you using? 您正在使用哪个版本的Framework? What installation/launch method are you using? 您正在使用哪种安装/启动方法? (ie, clickonce) And like the other poster suggested, have you checked the permissions on that folder on your C: drive? (即clickonce)和建议的其他海报一样,您是否检查了C:驱动器上该文件夹的权限? Right click the folder and check the security tab, make sure the logged in user has modify as well as read access to the folder. 右键单击该文件夹,然后检查“安全性”选项卡,确保已登录的用户具有对该文件夹的修改以及读取访问权限。

If you only need the file on a temporary basis, then look into writing to IsolatedStorage. 如果您只是临时需要该文件,则考虑将文件写入IsolatedStorage。 How to store and retrieve data in Isolated Storage File? 如何在隔离存储文件中存储和检索数据?

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

相关问题 UnauthorizedAccessException-拒绝访问路径 - UnauthorizedAccessException - Access to the path is denied UnauthorizedAccessException:对路径的访问被拒绝 - UnauthorizedAccessException: Access to the path is denied UnauthorizedAccessException:拒绝访问路径 - UnauthorizedAccessException: Access to path denied System.UnauthorizedAccessException:访问路径:拒绝PATH - System.UnauthorizedAccessException: Access to the path:PATH is denied DotNetZip System.UnauthorizedAccessException:拒绝访问路径 - DotNetZip System.UnauthorizedAccessException: Access to the path is denied Unity3D UnauthorizedAccessException:对路径的访问被拒绝 - Unity3D UnauthorizedAccessException: Access to the path is denied System.UnauthorizedAccessException:拒绝访问路径“...” - System.UnauthorizedAccessException: Access to the path '...' is denied System.UnauthorizedAccessException拒绝访问路径 - System.UnauthorizedAccessException Access to the path is denied System.UnauthorizedAccessException:访问路径被拒绝 - System.UnauthorizedAccessException: Access to the path denied 第一次机会异常访问路径被拒绝(UnauthorizedAccessException) - First Chance Exception access to path is denied (UnauthorizedAccessException)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM