当前位置: 首页>>代码示例>>C#>>正文


C# SftpClient.BeginUploadFile方法代码示例

本文整理汇总了C#中Renci.SshNet.SftpClient.BeginUploadFile方法的典型用法代码示例。如果您正苦于以下问题:C# SftpClient.BeginUploadFile方法的具体用法?C# SftpClient.BeginUploadFile怎么用?C# SftpClient.BeginUploadFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Renci.SshNet.SftpClient的用法示例。


在下文中一共展示了SftpClient.BeginUploadFile方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: UploadToSFTP

        public bool UploadToSFTP(MemoryStream ms, string filename, bool overwrite)
        {
            string strPath = sshPath;
              if (!strPath.StartsWith("/")) {
            strPath = "/" + strPath;
              }
              if (!strPath.EndsWith("/")) {
            strPath += "/";
              }

              bool bRet = true;
              bool bWait = true;

              sshError = "";

              if (sshConnection == null || !sshConnection.IsConnected) {
            bool bResume = false;
            bool bOK = false;
            connectionDoneCallback = (bool bSuccess) => {
              bOK = bSuccess;
              bResume = true;
            };
            Connect();
            while (!bResume) System.Threading.Thread.Sleep(1);
            if (!bOK) {
              sshError = "Couldn't connect to server.";
              return false;
            }
              }

              try {
            this.ProgressBar.Start(filename, ms.Length);
            ms.Seek(0, SeekOrigin.Begin);
            IAsyncResult arUpload = null;
            AsyncCallback cbFinished = (IAsyncResult ar) => {
              bRet = true;
              bWait = false;
            };
            Action<ulong> cbProgress = new Action<ulong>((ulong offset) => {
              this.ProgressBar.Set((long)offset);
              if (this.ProgressBar.Canceled) {
            sshConnection.EndUploadFile(arUpload);
            bRet = false;
            bWait = false;
              }
            });
            try {
              arUpload = sshConnection.BeginUploadFile(ms, strPath + filename, overwrite, cbFinished, filename, cbProgress);
            } catch {
              // failed to upload, queue it for a retry after reconnecting
              sshConnection = null;
              connectionDoneCallback = (bool bSuccess) => {
            if (!bSuccess) {
              bRet = false;
              sshError = "Upload failed because couldn't connect to server.";
              bWait = false;
              return;
            }
            try {
              arUpload = sshConnection.BeginUploadFile(ms, strPath + filename, overwrite, cbFinished, filename, cbProgress);
            } catch (Exception ex) {
              bRet = false;
              sshError = "Upload failed twice: " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
              bWait = false;
            }
              };
              Connect();
            }
              } catch (Exception ex) {
            bRet = false;
            bWait = false;
            sshError = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            throw ex;
              }

              while (bWait) System.Threading.Thread.Sleep(1);

              this.ProgressBar.Done();
              return bRet;
        }
开发者ID:jamesmanning,项目名称:Clipupload,代码行数:80,代码来源:SFTP.cs

示例2: UploadFilesFromFilemanager

                private void UploadFilesFromFilemanager()
                {
                    string destpathroot = txtRemoteFolderPath.Text;
                    if (!destpathroot.EndsWith("/"))
                    {
                        destpathroot += "/";
                    }
                    var filelist = new Dictionary<string, string>();
                    foreach (var item in lvLocalBrowser.SelectedItems.Cast<EXImageListViewItem>().Where(item => (string) item.Tag != "[..]"))
                    {
                        if ((string)item.Tag == "File")
                        {
                            filelist.Add(item.MyValue, destpathroot + Path.GetFileName(item.MyValue));
                        }
                        if ((string)item.Tag == "Folder")
                        {
                            string folder = Path.GetDirectoryName(item.MyValue);
                            folder = folder.EndsWith("\\") ? folder : folder + "\\";
                            string[] files = Directory.GetFiles(item.MyValue,
                                                                "*.*",
                                                                SearchOption.AllDirectories);

                            // Display all the files.
                            foreach (string file in files)
                            {
                                filelist.Add(Path.GetFullPath(file), destpathroot + Path.GetFullPath(file).Replace(folder,"").Replace("\\","/"));
                            }
                        }
                    }
                    long fulllength = filelist.Sum(file => new FileInfo(file.Key).Length);
                    MessageBox.Show(Tools.Misc.LengthToHumanReadable(fulllength));
                    var ssh = new SftpClient(txtHost.Text, int.Parse(this.txtPort.Text), txtUser.Text, txtPassword.Text);
                    ssh.Connect();
                    
                    ThreadPool.QueueUserWorkItem(state =>
                        {
                            long totaluploaded = 0;
                            foreach (var file in filelist)
                            {
                                CreatSSHDir(ssh, file.Value);
                                var s = new FileStream(file.Key, FileMode.Open);
                                var i = ssh.BeginUploadFile(s, file.Value) as SftpUploadAsyncResult;
                                while (!i.IsCompleted)
                                {
                                    SetProgressStatus(totaluploaded + (long)i.UploadedBytes, fulllength);
                                }
                                ssh.EndUploadFile(i);
                                totaluploaded += s.Length;
                            }
                            MessageBox.Show(Language.SSHTransfer_StartTransfer_Upload_completed_);
                            EnableButtons();
                            ssh.Disconnect();
                        });
                }
开发者ID:hmaster20,项目名称:mRemoteNC,代码行数:54,代码来源:UI.Window.SSHTransfer.cs

示例3: StartTransfer

                private void StartTransfer(SSHTransferProtocol Protocol)
                {
                    if (AllFieldsSet() == false)
                    {
                        Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg,
                                                            Language.strPleaseFillAllFields);
                        return;
                    }

                    if (File.Exists(this.txtLocalFile.Text) == false)
                    {
                        Runtime.MessageCollector.AddMessage(Messages.MessageClass.WarningMsg,
                                                            Language.strLocalFileDoesNotExist);
                        return;
                    }

                    try
                    {
                        if (Protocol == SSHTransferProtocol.SCP)
                        {
                            var ssh = new ScpClient(txtHost.Text, int.Parse(this.txtPort.Text), txtUser.Text, txtPassword.Text);
                            ssh.Uploading+=(sender, e) => SetProgressStatus(e.Uploaded, e.Size);
                            DisableButtons();
                            ssh.Connect();
                            ssh.Upload(new FileStream(txtLocalFile.Text,FileMode.Open), txtRemoteFile.Text);
                        }
                        else if (Protocol == SSHTransferProtocol.SFTP)
                        {
                            var ssh = new SftpClient(txtHost.Text, int.Parse(txtPort.Text),txtUser.Text, txtPassword.Text);
                            var s = new FileStream(txtLocalFile.Text, FileMode.Open);
                            ssh.Connect();
                            var i = ssh.BeginUploadFile(s, txtRemoteFile.Text) as SftpUploadAsyncResult;
                            ThreadPool.QueueUserWorkItem(state => 
                            {
                                while (!i.IsCompleted)
                                {
                                    SetProgressStatus((long)i.UploadedBytes, s.Length);
                                }
                                MessageBox.Show(Language.SSHTransfer_StartTransfer_Upload_completed_);
                                EnableButtons();
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg,
                                                            Language.strSSHTransferFailed + Constants.vbNewLine +
                                                            ex.Message);
                        EnableButtons();
                    }
                }
开发者ID:hmaster20,项目名称:mRemoteNC,代码行数:51,代码来源:UI.Window.SSHTransfer.cs

示例4: go

        public void go(string filename)
        {
            if (string.IsNullOrWhiteSpace(filename) || connInfo == null || config.sftpEnabled == false)
            {
                Log.Debug("Not doing SFTP because it wasn't configured properly or at all");
                return;
            }

            FileInfo fi = new FileInfo(filename);
            if(!fi.Exists)
            {
                Log.Error("Can't open file for SFTPing: " + filename);
                return;
            }

            if(!fi.DirectoryName.StartsWith(config.localBaseFolder))
            {
                Log.Error("Can't figure out where the file " + filename + " is relative to the base dir");
                return;
            }

            string rel = fi.DirectoryName.Replace(config.localBaseFolder, "");
            if (rel.StartsWith(Path.DirectorySeparatorChar.ToString()))
                rel = rel.Substring(1);

            SftpClient client = new SftpClient(connInfo);
            string accum = "";
            try
            {
                client.Connect();
                string thedir = null;
                foreach (string str in rel.Split(Path.DirectorySeparatorChar))
                {
                    accum = accum + "/" + str;
                    thedir = config.sftpRemoteFolder + "/" + accum;
                    thedir = thedir.Replace("//", "/");
                    Log.Debug("Trying to create directory " + thedir);
                    try
                    {
                        client.CreateDirectory(thedir);
                    }
                    catch (SshException) { }
                }
                FileStream fis = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    client.BeginUploadFile(fis, thedir + "/" + fi.Name, true, (fini) =>
                    {
                        FileStream ffini = fini.AsyncState as FileStream;
                        if (ffini != null)
                            ffini.Close();
                        if (client != null && client.IsConnected)
                        {
                            client.Disconnect();
                        }
                        Log.Debug("Upload finished!");
                        if(Program.frm != null)
                            Program.frm.SetStatus("Upload finished! / Ready");
                    }, fis, (pct) =>
                    {
                        if(Program.frm != null)
                        {
                            Program.frm.SetStatus("Uploaded " + pct.ToString() + " bytes");
                        }
                    });
            }
            catch(Exception aiee)
            {
                Log.Error("Error: " + aiee.Message);
                Log.Debug(aiee.StackTrace);
            }
        }
开发者ID:Elusive138,项目名称:YASDown,代码行数:70,代码来源:SftpUploader.cs


注:本文中的Renci.SshNet.SftpClient.BeginUploadFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。