2016-03-01 2 views
0

Cela fait quelques jours que je travaille dessus et je n'arrive pas à comprendre pourquoi je ne suis pas capable de télécharger tous les fichiers du répertoire depuis mon serveur. Je suis en mesure de télécharger le tout premier fichier, puis après avoir obtenu une erreur d'accès refusé.Télécharger tous les fichiers à partir d'un répertoire de serveur à l'aide de Renci.SshNet

Voici le code que j'utilise pour me connecter au serveur et démarrer le processus de téléchargement.

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(); 
     } 
    } 

ci-dessous est l'exception entière I recive lors de l'exécution:

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) 

J'ai essayé d'ajouter un fichier à la filestream et je ne reçois pas une erreur, mais il ne cesse de remplacer le seul fichier Me laissant avec le dernier fichier dans le répertoire.

Toute aide serait grandement appréciée. Merci d'avance.

+2

'C: \ Utilisateurs \ ***** \ Documents \ Visual Studio 2015 \ Projets \ ******** \ ********** \ bin \ Debug \ *** * \ Invoices' est où l'erreur est et il ressemble à un dossier et non un fichier. Vous devez modifier votre code pour ignorer les dossiers (ou parcourir les dossiers). – Icemanind

+0

utilisez le débogueur .. Etes-vous sûr que la connexion est toujours ouverte lorsque vous arrivez à la 2ème itération? essayez de vérifier l'état de la connexion sftp. aussi où sont les factures déclarées dans cette ligne 'foreach (var fichier dans les factures)' – MethodMan

+0

Comment ferais-je cela? –

Répondre

1

L'erreur est due au fait que vous extrayez des répertoires en plus des fichiers. Je voudrais juste envelopper votre code dans un bloc try et catch l'exception UnauthorizedAccessException et placez un continue à l'intérieur de votre bloc catch.