示例#1
0
文件: FTP.cs 项目: yomigits/ShareX
        public bool UploadData(Stream localStream, string remotePath)
        {
            if (Connect())
            {
                try
                {
                    using (Stream remoteStream = client.OpenWrite(remotePath))
                    {
                        return(TransferData(localStream, remoteStream));
                    }
                }
                catch (FtpCommandException e)
                {
                    // Probably directory not exist, try creating it
                    if (e.CompletionCode == "550" || e.CompletionCode == "553")
                    {
                        CreateMultiDirectory(URLHelpers.GetDirectoryPath(remotePath));

                        using (Stream remoteStream = client.OpenWrite(remotePath))
                        {
                            return(TransferData(localStream, remoteStream));
                        }
                    }

                    throw e;
                }
            }

            return(false);
        }
示例#2
0
        public void RemoveDirectory(string url)
        {
            string filename = URLHelpers.GetFileName(url);

            if (filename == "." || filename == "..")
            {
                return;
            }

            List <FTPLineResult> files = ListDirectoryDetails(url);
            string path = URLHelpers.GetDirectoryPath(url);

            foreach (FTPLineResult file in files)
            {
                if (file.IsDirectory)
                {
                    RemoveDirectory(URLHelpers.CombineURL(url, file.Name));
                }
                else
                {
                    DeleteFile(URLHelpers.CombineURL(url, file.Name));
                }
            }

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);

            request.Proxy       = Options.ProxySettings;
            request.Method      = WebRequestMethods.Ftp.RemoveDirectory;
            request.Credentials = new NetworkCredential(Options.Account.Username, Options.Account.Password);
            request.KeepAlive   = false;

            request.GetResponse();

            WriteOutput("RemoveDirectory: " + url);
        }
示例#3
0
        private bool UploadStream(Stream stream, string remotePath, bool autoCreateDirectory = false)
        {
            if (Connect())
            {
                try
                {
                    using (SftpFileStream sftpStream = client.Create(remotePath))
                    {
                        return(TransferData(stream, sftpStream));
                    }
                }
                catch (SftpPathNotFoundException) when(autoCreateDirectory)
                {
                    // Happens when directory not exist, create directory and retry uploading

                    CreateDirectory(URLHelpers.GetDirectoryPath(remotePath), true);
                    return(UploadStream(stream, remotePath));
                }
                catch (NullReferenceException)
                {
                    // Happens when disconnect while uploading
                }
            }

            return(false);
        }
示例#4
0
文件: FTP.cs 项目: zz110/ShareX
        public bool UploadData(Stream localStream, string remotePath)
        {
            if (Connect())
            {
                try
                {
                    return(UploadData2(localStream, remotePath));
                }
                catch (FtpCommandException e)
                {
                    // Probably directory not exist, try creating it
                    if (e.CompletionCode == "550" || e.CompletionCode == "553")
                    {
                        CreateMultiDirectory(URLHelpers.GetDirectoryPath(remotePath));

                        return(UploadData2(localStream, remotePath));
                    }

                    throw e;
                }
            }

            return(false);
        }
示例#5
0
        private bool UploadStream(Stream stream, string remotePath)
        {
            if (Connect())
            {
                try
                {
                    using (SftpFileStream sftpStream = client.OpenWrite(remotePath))
                    {
                        return(TransferData(stream, sftpStream));
                    }
                }
                catch (SftpPathNotFoundException)
                {
                    CreateDirectory(URLHelpers.GetDirectoryPath(remotePath));

                    using (SftpFileStream sftpStream = client.OpenWrite(remotePath))
                    {
                        return(TransferData(stream, sftpStream));
                    }
                }
            }

            return(false);
        }