2017-08-23 37 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がディレクトリであることが表示されます:ディレクトリパスにI/Oをしようと

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

UnauthorizedAccessExceptionを発生させます。

コールでファイル名を指定してください。

+0

C:\ Users \ HIKMET \ Desktop \ Proje_XML_to_SCADA \のように 'download()'コールにファイル名を指定しています。ダウンロードしたXML ""、誤解をおかけして申し訳ありません。 –

+1

「DownloadedXML」はファイル名ですか? 'C:\ Users \ HIKMET \ Desktop \ Proje_XML_to_SCADA \ DownloadedXML \ something.xml'とは対照的ですか? –

+0

はい、あなたは正しいです!どうもありがとうございます! –

関連する問題