Esempio n. 1
0
        public bool ChangeDirectory(string remotePath, bool autoCreateDirectory = false)
        {
            if (Connect())
            {
                remotePath = FTPHelpers.AddSlash(remotePath, FTPHelpers.SlashType.Prefix);

                try
                {
                    Client.ChangeDirectory(remotePath);
                    return(true);
                }
                catch (Exception e)
                {
                    if (autoCreateDirectory && e.Message.StartsWith("Could not change working directory to"))
                    {
                        MakeMultiDirectory(remotePath);
                        Client.ChangeDirectory(remotePath);
                        return(true);
                    }

                    throw e;
                }
            }

            return(false);
        }
Esempio n. 2
0
        public string[] ListDirectory(string url)
        {
            List <string> result = new List <string>();

            url = FTPHelpers.AddSlash(url, FTPHelpers.SlashType.Suffix);

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

            request.Proxy       = this.Options.ProxySettings;
            request.Method      = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = new NetworkCredential(this.Options.Account.UserName, this.Options.Account.Password);
            request.KeepAlive   = false;
            request.Timeout     = 10000;
            request.UsePassive  = !this.Options.Account.IsActive;

            using (WebResponse response = request.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    while (!reader.EndOfStream)
                    {
                        result.Add(reader.ReadLine());
                    }

                    WriteOutput("ListDirectory: " + url);

                    return(result.ToArray());
                }
        }
        public bool Test(string remotePath)
        {
            if (Connect())
            {
                remotePath = FTPHelpers.AddSlash(remotePath, FTPHelpers.SlashType.Prefix);
                try
                {
                    Client.ChangeDirectory(remotePath);
                }
                catch (Exception ftpResponse)
                {
                    if (ftpResponse.InnerException.Message.Contains("No such file or directory"))
                    {
                        Client.MakeDirectory(remotePath);
                        Client.ChangeDirectory(remotePath);
                    }
                }

                return(true);
            }
            return(false);
        }
Esempio n. 4
0
        private void lvFTPList_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(string[])))
            {
                Point        point = lvFTPList.PointToClient(new Point(e.X, e.Y));
                ListViewItem lvi   = lvFTPList.GetItemAt(point.X, point.Y);
                if (lvi != null && e.AllowedEffect == DragDropEffects.Move)
                {
                    if (tempSelected != null && tempSelected != lvi)
                    {
                        tempSelected.Selected = false;
                    }

                    FtpItem file = lvi.Tag as FtpItem;
                    if (file != null && file.ItemType == FtpItemType.Directory)
                    {
                        string[] filenames = e.Data.GetData(typeof(string[])) as string[];
                        if (filenames != null)
                        {
                            int renameCount = 0;
                            foreach (string filename in filenames)
                            {
                                if (file.Name != filename)
                                {
                                    string path     = Helpers.CombineURL(currentDirectory, filename);
                                    string movePath = string.Empty;
                                    if (file.ItemType == FtpItemType.Unknown)
                                    {
                                        if (file.Name == ".")
                                        {
                                            movePath = FTPHelpers.AddSlash(filename, FTPHelpers.SlashType.Prefix, 2);
                                        }
                                        else if (file.Name == "..")
                                        {
                                            movePath = FTPHelpers.AddSlash(filename, FTPHelpers.SlashType.Prefix);
                                        }
                                    }
                                    else
                                    {
                                        movePath = Helpers.CombineURL(file.FullPath, filename);
                                    }

                                    if (!string.IsNullOrEmpty(movePath))
                                    {
                                        FTPAdapter.Rename(path, movePath);
                                        renameCount++;
                                    }
                                }
                            }

                            if (renameCount > 0)
                            {
                                RefreshDirectory();
                            }
                        }
                    }
                }

                tempSelected = null;
            }
            else if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
                if (files != null)
                {
                    FTPAdapter.UploadFiles(files, currentDirectory);
                    RefreshDirectory();
                }
            }
        }