【问题标题】:Download every file from a server directory using Renci.SshNet使用 Renci.SshNet 从服务器目录下载每个文件
【发布时间】:2016-06-14 04:26:06
【问题描述】:

我已经为此工作了几天,似乎无法弄清楚为什么我无法从我的服务器下载目录中的每个文件。在收到拒绝访问错误后,我可以成功下载第一个文件。

下面是我用来连接服务器并开始下载过程的代码。

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

以下是我在运行时收到的整个异常:

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)

我尝试将文件添加到文件流中,但没有收到错误消息,但它一直在替换一个文件,只留下目录中的最后一个文件。

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • C:\Users\*****\Documents\Visual Studio 2015\Projects\********\**********\bin\Debug\****\Invoices 是错误所在,它看起来像是一个文件夹而不是文件。您需要修改代码以跳过文件夹(或遍历文件夹)。
  • 使用调试器。我确定在第二次迭代时连接仍然打开吗?尝试检查 sftp 连接状态。此行中声明的发票在哪里foreach (var file in invoices)
  • 我该怎么做?
  • 编辑了我的问题忘了添加那部分
  • 我要做的就是将你的代码包装在你的 foreach 块内的 try 块和 catch UnauthorizedAccessException 异常中,并且只是 continue 里面的循环。

标签: c# winforms openssh


【解决方案1】:

该错误是因为您在获取文件之外的目录。我只需将您的代码包装在 try 块和 catch UnauthorizedAccessException 异常中,然后将 continue 放在您的 catch 块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多