2016-03-01 56 views
0

我一直在這工作了幾天,似乎無法弄清楚爲什麼我無法下載目錄中的每個文件我的服務器。在獲得訪問被拒絕錯誤後,我成功地能夠下載第一個文件。使用Renci.ShNet從服務器目錄中下載每個文件

下面是我用來連接到服務器並開始下載過程的代碼。

public void downloadPaperwork() 
    { 
     // Setup Credentials and Server Information 
     ConnectionInfo ConnNfo = new ConnectionInfo(ipAddress, port, serverName, 
      new AuthenticationMethod[]{ 
      // Key Based Authentication (using keys in OpenSSH Format) 
      new PrivateKeyAuthenticationMethod(serverName,new PrivateKeyFile[]{ 
       new PrivateKeyFile(keyFile,password) 
      }), 
      }); 

     using (var sftp = new SftpClient(ConnNfo)) 
     { 
      sftp.Connect(); 

      sftp.ChangeDirectory("/var/www/html/invoices"); 

      List<SftpFile> invoices = sftp.ListDirectory(".").ToList(); 

      foreach (var file in invoices) 
      { 
       string filename = Path.Combine(Application.StartupPath + folder, file.Name); 

       using (var fs = new FileStream(filename, FileMode.Create)) 
       { 
        if(!file.Name.Equals(".") && !file.Name.Equals("..")) 
        { 
         MessageBox.Show(file.Name); 
         sftp.DownloadFile(file.FullName, fs); 
         fs.Close(); 
        } 

       } 
      } 
      sftp.Disconnect(); 
     } 
    } 

下面是整個例外,在運行時我recive:

System.UnauthorizedAccessException: Access to the path 'C:\Users\*****\Documents\Visual Studio 2015\Projects\********\**********\bin\Debug\****\Invoices' is denied. 
    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, Boolean useLongPath, Boolean checkHost) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 
    at System.IO.FileStream..ctor(String path, FileMode mode) 
    at *********.Form1.downloadPaperwork() in C:\Users\******\Documents\Visual Studio 2015\Projects\*********\*********\Secretary Form.cs:line 173 
    at ********.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\*****\Documents\Visual Studio 2015\Projects\*******\*******\Secretary Form.cs:line 120 
    at System.Windows.Forms.Form.OnLoad(EventArgs e) 
    at System.Windows.Forms.Form.OnCreateControl() 
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 
    at System.Windows.Forms.Control.CreateControl() 
    at System.Windows.Forms.Control.WmShowWindow(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
    at System.Windows.Forms.Form.WmShowWindow(Message& m) 
    at System.Windows.Forms.Form.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

我試着將文件添加到文件流,我沒有得到一個錯誤,但它使替換一個文件只留下目錄中的最後一個文件。

任何幫助將不勝感激。提前致謝。

+2

'C:\ Users \ ***** \ Documents \ Visual Studio 2015 \ Projects \ ******** \ ********** \ bin \ Debug \ *** * \ Invoices'是錯誤的地方,它看起來像一個文件夾而不是文件。您需要修改代碼以跳過文件夾(或遍歷文件夾)。 – Icemanind

+0

使用調試器..我確定在第二次迭代時連接仍然打開?嘗試檢查sftp連接狀態。也是在這行'foreach(發票var文件)聲明發票' – MethodMan

+0

我該怎麼做呢? –

回答

1

錯誤是因爲除了文件之外您正在提取目錄。我只是將您的代碼包含在try塊中,catchUnauthorizedAccessException例外,並將continue放入您的catch塊中。