Example #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);
        }
Example #2
0
        public void RemoveDirectory(string url)
        {
            string filename = FTPHelpers.GetFileName(url);

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

            List <FTPLineResult> files = ListDirectoryDetails(url);
            string path = FTPHelpers.GetDirectoryName(url);

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

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

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

            request.GetResponse();

            WriteOutput("RemoveDirectory: " + url);
        }
Example #3
0
        public void DeleteDirectory(string remotePath)
        {
            Connect();

            string filename = FTPHelpers.GetFileName(remotePath);

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

            FtpItemCollection files = GetDirList(remotePath);

            foreach (FtpItem file in files)
            {
                if (file.ItemType == FtpItemType.Directory)
                {
                    DeleteDirectory(file.FullPath);
                }
                else
                {
                    DeleteFile(file.FullPath);
                }
            }

            Client.DeleteDirectory(remotePath);
        }
Example #4
0
        public string GetUriPath(string filename)
        {
            if (string.IsNullOrEmpty(Host))
            {
                return(string.Empty);
            }

            if (HttpHomePathNoExtension)
            {
                filename = Path.GetFileNameWithoutExtension(filename);
            }

            string path;

            HttpHomePath = FTPHelpers.RemovePrefixes(HttpHomePath);
            NameParser nameParser   = new NameParser(NameParserType.URL);
            string     httpHomePath = nameParser.Parse(HttpHomePath.Replace("%host", Host));

            string subFolderPath   = GetSubFolderPath();
            string browserProtocol = BrowserProtocol.GetDescription();

            if (string.IsNullOrEmpty(httpHomePath))
            {
                string host = Host;

                if (host.StartsWith("ftp."))
                {
                    host = host.Substring(4);
                }

                path = Helpers.CombineURL(host, subFolderPath, filename);
            }
            else
            {
                if (!httpHomePath.StartsWith("@"))
                {
                    path = Helpers.CombineURL(httpHomePath, subFolderPath);
                }
                else
                {
                    path = httpHomePath.Substring(1);
                }

                if (path.EndsWith("="))
                {
                    path += filename;
                }
                else
                {
                    path = Helpers.CombineURL(path, filename);
                }
            }

            if (!path.StartsWith(browserProtocol))
            {
                path = browserProtocol + path;
            }

            return(path);
        }
Example #5
0
        public string GetHttpHomePath()
        {
            HttpHomePath = FTPHelpers.RemovePrefixes(HttpHomePath);
            NameParser nameParser = new NameParser(NameParserType.URL);

            return(nameParser.Parse(HttpHomePath.Replace("%host", Host)));
        }
Example #6
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 string GetHttpHomePath()
        {
            NameParser parser = new NameParser(NameParserType.URL);

            HttpHomePath = FTPHelpers.RemovePrefixes(HttpHomePath.Replace("%host", LocalhostRoot));
            return(parser.Parse(HttpHomePath));
        }
        public string GetUriPath(string fileName, bool customPath)
        {
            if (string.IsNullOrEmpty(this.Host))
            {
                return(string.Empty);
            }

            string path          = string.Empty;
            string host          = this.Host;
            string lHttpHomePath = GetHttpHomePath();
            string lFolderPath   = GetSubFolderPath();

            if (HttpHomePathNoExtension)
            {
                int index = fileName.LastIndexOf('.');
                if (index > -1)
                {
                    fileName = fileName.Remove(index);
                }
            }

            if (host.StartsWith("ftp."))
            {
                host = host.Remove(0, 4);
            }

            if (lHttpHomePath.StartsWith("@") || customPath)
            {
                lFolderPath = string.Empty;
            }

            if (string.IsNullOrEmpty(lHttpHomePath))
            {
                path = FTPHelpers.CombineURL(host, lFolderPath, fileName);
            }
            else
            {
                string httppath = lHttpHomePath.Replace("%host", host).TrimStart('@');
                path = FTPHelpers.CombineURL(httppath, lFolderPath, fileName);
            }

            if (!path.StartsWith(BrowserProtocol.GetDescription()))
            {
                switch (BrowserProtocol)
                {
                case UploadersLib.BrowserProtocol.ServerProtocol:
                    path = FTPHelpers.CombineURL(FTPAddress, lFolderPath, fileName);
                    break;

                default:
                    path = BrowserProtocol.GetDescription() + path;
                    break;
                }
            }

            return(path);
        }
        public void MakeMultiDirectory(string remotePath)
        {
            List <string> paths = FTPHelpers.GetPaths(remotePath);

            foreach (string path in paths)
            {
                MakeDirectory(path);
            }
        }
Example #10
0
        public string GetLocalhostUri(string fileName)
        {
            string LocalhostAddress = this.LocalUri;

            if (string.IsNullOrEmpty(LocalhostAddress))
            {
                return(string.Empty);
            }

            return(FTPHelpers.CombineURL(LocalhostAddress, this.GetSubFolderPath(), fileName));
        }
        public string GetFtpPath(string fileName)
        {
            string ftpAddress = this.FTPAddress;

            if (string.IsNullOrEmpty(ftpAddress))
            {
                return(string.Empty);
            }

            return(FTPHelpers.CombineURL(ftpAddress, this.GetSubFolderPath(), fileName));
        }
Example #12
0
        public void MakeMultiDirectory(string remotePath)
        {
            List <string> paths = FTPHelpers.GetPaths(remotePath);

            foreach (string path in paths)
            {
                if (MakeDirectory(path))
                {
                    DebugHelper.WriteLine("FTP MakeDirectory: " + path);
                }
            }
        }
Example #13
0
        public string GetHttpHomePath()
        {
            // @ deprecated
            if (HttpHomePath.StartsWith("@"))
            {
                HttpHomePath = HttpHomePath.Substring(1);
                HttpHomePathAutoAddSubFolderPath = false;
            }

            HttpHomePath = FTPHelpers.RemovePrefixes(HttpHomePath);

            NameParser nameParser = new NameParser(NameParserType.URL);

            return(nameParser.Parse(HttpHomePath.Replace("%host", Host)));
        }
Example #14
0
        public void MakeMultiDirectory(string dirName)
        {
            string path = "";

            string[] dirs = dirName.Split('/');
            foreach (string dir in dirs)
            {
                if (!string.IsNullOrEmpty(dir))
                {
                    path = FTPHelpers.CombineURL(path, dir);
                    MakeDirectory(FTPHelpers.CombineURL(Options.Account.FTPAddress, path));
                }
            }

            WriteOutput("MakeMultiDirectory: " + dirName);
        }
Example #15
0
        private void FillDirectories(string path)
        {
            List <string> paths = FTPHelpers.GetPaths(path);

            paths.Insert(0, "/");

            cbDirectoryList.Items.Clear();
            foreach (string directory in paths)
            {
                cbDirectoryList.Items.Add(directory);
            }

            if (cbDirectoryList.Items.Count > 0)
            {
                cbDirectoryList.SelectedIndex = cbDirectoryList.Items.Count - 1;
            }
        }
Example #16
0
        public string GetUriPath(string fileName, bool customPath)
        {
            if (string.IsNullOrEmpty(this.LocalhostRoot))
            {
                return(string.Empty);
            }

            fileName = HttpUtility.UrlEncode(fileName).Replace("+", "%20");
            string path          = string.Empty;
            string httppath      = string.Empty;
            string lHttpHomePath = GetHttpHomePath();

            if (string.IsNullOrEmpty(lHttpHomePath))
            {
                RemoteProtocol = BrowserProtocol.File;
            }
            else if (!string.IsNullOrEmpty(lHttpHomePath) && RemoteProtocol == BrowserProtocol.File)
            {
                RemoteProtocol = BrowserProtocol.Http;
            }

            string lFolderPath = this.GetSubFolderPath();

            if (lHttpHomePath.StartsWith("@") || customPath)
            {
                lFolderPath = string.Empty;
            }

            if (string.IsNullOrEmpty(lHttpHomePath))
            {
                httppath = LocalUri.Replace("file://", "");
            }
            else
            {
                httppath = lHttpHomePath.Replace("%host", this.LocalhostRoot).TrimStart('@');
            }
            path = FTPHelpers.CombineURL(this.Port == 80 ? httppath : string.Format("{0}:{1}", httppath, this.Port), lFolderPath, fileName);

            if (!path.StartsWith(RemoteProtocol.GetDescription()))
            {
                path = RemoteProtocol.GetDescription() + path;
            }

            return(path);
        }
Example #17
0
        public void DeleteFile(string url)
        {
            string filename = FTPHelpers.GetFileName(url);

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

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

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

            request.GetResponse();

            WriteOutput("DeleteFile: " + url);
        }
        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);
        }
 public void UploadFiles(string[] localPaths, string remotePath)
 {
     foreach (string file in localPaths)
     {
         if (!string.IsNullOrEmpty(file))
         {
             string filename = Path.GetFileName(file);
             if (File.Exists(file))
             {
                 UploadFile(file, FTPHelpers.CombineURL(remotePath, filename));
             }
             else if (Directory.Exists(file))
             {
                 List <string> filesList = new List <string>();
                 filesList.AddRange(Directory.GetFiles(file));
                 filesList.AddRange(Directory.GetDirectories(file));
                 string path = FTPHelpers.CombineURL(remotePath, filename);
                 MakeDirectory(path);
                 UploadFiles(filesList.ToArray(), path);
             }
         }
     }
 }
Example #20
0
        public void UploadData(Stream stream, string remotePath)
        {
            if (Connect())
            {
                progress = new ProgressManager(stream.Length);

                try
                {
                    Client.PutFile(stream, remotePath, FileAction.Create);
                }
                catch (Exception e)
                {
                    if (e.InnerException.Message.Contains("No such file or directory"))
                    {
                        MakeMultiDirectory(FTPHelpers.GetDirectoryName(remotePath));
                        Client.PutFile(stream, remotePath, FileAction.Create);
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
        }
Example #21
0
        public static void TestFTPAccount(FTPAccount account, bool silent)
        {
            string msg = string.Empty;
            string sfp = account.GetSubFolderPath();

            switch (account.Protocol)
            {
            case FTPProtocol.SFTP:
                SFTP sftp = new SFTP(account);
                if (!sftp.IsInstantiated)
                {
                    msg = "An SFTP client couldn't be instantiated, not enough information.\nCould be a missing key file.";
                }
                else if (sftp.Connect())
                {
                    List <string> createddirs = new List <string>();
                    if (!sftp.DirectoryExists(sfp))
                    {
                        createddirs = sftp.CreateMultipleDirectorys(FTPHelpers.GetPaths(sfp));
                    }
                    if (sftp.IsConnected)
                    {
                        msg = (createddirs.Count == 0) ? "Connected!" : "Conected!\nCreated folders;\n";
                        for (int x = 0; x <= createddirs.Count - 1; x++)
                        {
                            msg += createddirs[x] + "\n";
                        }
                        msg += " \n\nPing results:\n " + SendPing(account.Host, 3);
                        sftp.Disconnect();
                    }
                }
                break;

            default:
                using (FTP ftpClient = new FTP(account))
                {
                    try
                    {
                        //DateTime time = DateTime.Now;
                        ftpClient.Test(sfp);
                        msg = "Success!";
                    }
                    catch (Exception e)
                    {
                        if (e.Message.StartsWith("Could not change working directory to"))
                        {
                            try
                            {
                                ftpClient.MakeMultiDirectory(sfp);
                                ftpClient.Test(sfp);
                                msg = "Success!\nAuto created folders: " + sfp;
                            }
                            catch (Exception e2)
                            {
                                msg = e2.Message;
                            }
                        }
                        else
                        {
                            msg = e.Message;
                        }
                    }
                }

                if (!string.IsNullOrEmpty(msg))
                {
                    string ping = SendPing(account.Host, 3);
                    if (!string.IsNullOrEmpty(ping))
                    {
                        msg += "\n\nPing results:\n" + ping;
                    }

                    if (silent)
                    {
                        // Engine.MyLogger.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account.ToString()));
                    }
                    else
                    {
                        //MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                break;
            }
            if (silent)
            {
                DebugHelper.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account.ToString()));
            }
            else
            {
                MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Example #22
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();
                }
            }
        }