2017-08-23 385 views
0

我试图从ftp服务器下载.xml文件到我的电脑,但是我总是接收一个错误,因为System.UnauthorizedAccessException:C:\users\...\mypath访问被拒绝。通过FTP下载文件

我该如何解决清楚的问题?我正在尝试将目标文件夹更改为可移动硬盘,不幸的是我得到了相同的异常。此外,我无法更改只读状态的文件夹属性,我在哪里犯了我无法获得的错误。你能帮忙解决问题并解决它吗?

控制台说FileStream localFileStream = new FileStream(localFile, FileMode.Create);代码部分是错误的。

using System; 
using System.IO; 
using System.Net; 
using System.Security.Permissions; 
using System.Security.Authentication; 
using System.Security.AccessControl; 

namespace GetXMLNodes 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 
      ftp ftpClient = new ftp(@"ftp://...ip", "username", "password"); 
      ftpClient.download(@"3636594381842_2015-04-08T12_08_27.177Z Status.xml", @"C:\Users\HIKMET\Desktop\Proje_XML_to_SCADA\DownloadedXML"); 
      ftpClient = null; 
      Console.WriteLine("download success..!"); 
      Console.ReadLine(); 
     } 

     catch(Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 
} 



class ftp 
{ 
    private string host = null; 
    private string user = null; 
    private string pass = null; 
    private FtpWebRequest ftpRequest = null; 
    private FtpWebResponse ftpResponse = null; 
    private Stream ftpStream = null; 
    private int bufferSize = 2048; 

    /* Construct Object */ 
    public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; } 

    /* Download File */ 
    public void download(string remoteFile, string localFile) 
    { 
     try 
     { 
      /* Create an FTP Request */ 
      ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); 
      /* Log in to the FTP Server with the User Name and Password Provided */ 
      ftpRequest.Credentials = new NetworkCredential(user, pass); 
      /* When in doubt, use these options */ 
      ftpRequest.UseBinary = true; 
      ftpRequest.UsePassive = true; 
      ftpRequest.KeepAlive = true; 
      /* Specify the Type of FTP Request */ 
      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
      /* Establish Return Communication with the FTP Server */ 
      ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
      /* Get the FTP Server's Response Stream */ 
      ftpStream = ftpResponse.GetResponseStream(); 
      /* Open a File Stream to Write the Downloaded File */ 
      //FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write,localFile); 
      //f.AllLocalFiles = FileIOPermissionAccess.Write; 
      //f.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, localFile); 
      //f.Demand(); 
      FileStream localFileStream = new FileStream(localFile, FileMode.Create); 
      /* Buffer for the Downloaded Data */ 
      byte[] byteBuffer = new byte[bufferSize]; 
      int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); 
      /* Download the File by Writing the Buffered Data Until the Transfer is Complete */ 
      try 
      { 
       while (bytesRead > 0) 
       { 
        localFileStream.Write(byteBuffer, 0, bytesRead); 
        bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); 
       } 
      } 
      catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
      /* Resource Cleanup */ 
      localFileStream.Close(); 
      ftpStream.Close(); 
      ftpResponse.Close(); 
      ftpRequest = null; 
     } 
     catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
     return; 
    } 

}

+1

你说这个文件夹是只读的,你不能改变它,但你试图将一个新文件保存到它并获得未经授权的访问异常。当你保存某个你永远可以访问文档的地方时会发生什么?它是否有效? – Equalsk

+0

不幸的是,我得到了同样的错误,同样的例外 –

回答

2

看来,localFile是一个目录:

@"C:\Users\HIKMET\Desktop\Proje_XML_to_SCADA\DownloadedXML" 

的目录路径上尝试I/O会提高UnauthorizedAccessException

download()调用中提供文件名。

+0

对不起,我不明白你的意思,我已经提供文件名到'download()'调用为'@“C:\ Users \ HIKMET \ Desktop \ Proje_XML_to_SCADA \ DownloadedXML“',对于误解抱歉,你能否用更多细节解释解决方案? –

+1

所以'DownloadedXML'是文件名?而不是'C:\ Users \ HIKMET \ Desktop \ Proje_XML_to_SCADA \ DownloadedXML \ something.xml'? –

+0

是的,你是对的!非常感谢! –