Beispiel #1
0
 public void changePermissions(string Path, string Permissions, string HostName, string UserName, string Password)
 {
     FluentFTP.FtpClient client = new FluentFTP.FtpClient(HostName);
     client.Credentials = new NetworkCredential(UserName, Password);
     client.Connect();
     client.Chmod(Path, Convert.ToInt32(Permissions));
     client.Disconnect();
 }
Beispiel #2
0
        static void TestClient()
        {
            string host     = ConfigurationManager.AppSettings["FTP_HOST"];
            int    port     = int.Parse(ConfigurationManager.AppSettings["FTP_PORT"]);
            string username = ConfigurationManager.AppSettings["FTP_USERNAME"];
            string password = ConfigurationManager.AppSettings["FTP_PASSWORD"];

            using (FileStream stream = File.Open("C:\\ftp.txt", FileMode.Open))
            {
                FluentFTP.FtpClient client = new FluentFTP.FtpClient(host, port, username, password);

                client.Connect();
                client.Upload(stream, $"/ftp.txt", FluentFTP.FtpExists.Overwrite, true);
                client.Disconnect();

                stream.Close();
            }
        }
Beispiel #3
0
 public void runFluent(int command)
 {
     //0 = change permissions
     if (command == 0)
     {
         try
         {
             FluentFTP.FtpClient client = new FluentFTP.FtpClient(this.connection.ServerName);
             client.Credentials = new System.Net.NetworkCredential(this.connection.UserName, this.connection.PassWord);
             client.Connect();
             client.Chmod(this.path, this.permission);
             client.Disconnect();
         }
         catch (Exception e)
         {
             throw e;
         }
     }
 }
Beispiel #4
0
        static void SaveFileToFtp(string sessionId, string userId, string toSave, string extName)
        {
            string filename = ConfigurationManager.AppSettings["SSO_ROOT"] + sessionId;

            string host       = ConfigurationManager.AppSettings["FTP_HOST"];
            int    port       = int.Parse(ConfigurationManager.AppSettings["FTP_PORT"]);
            string username   = ConfigurationManager.AppSettings["FTP_USERNAME"];
            string password   = ConfigurationManager.AppSettings["FTP_PASSWORD"];
            string remoteRoot = ConfigurationManager.AppSettings["REMOTE_ROOT"];

            using (FileStream stream = File.Open(filename, FileMode.Open))
            {
                FluentFTP.FtpClient client = new FluentFTP.FtpClient(host, port, username, password);

                client.Connect();
                client.Upload(stream, $"{remoteRoot}{userId}/{userId}_{toSave}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.{extName}", FluentFTP.FtpExists.Overwrite, true);
                client.Disconnect();

                stream.Close();
            }

            File.Delete(filename);
        }
Beispiel #5
0
        /// <summary>
        /// 导航到指定的地址。需要登录时自动弹出登录界面。导航失败时,自动导航到失败页面。
        /// 不抛出异常。
        /// </summary>
        /// <param name="address">要导航到的地址</param>
        /// <returns>导航是否成功</returns>
        private async Task <bool> NavigateAsync(Uri address)
        {
            await ftpSemaphore.WaitAsync();

            try
            {
                errorMessage.Visibility     = Visibility.Collapsed;
                progressBar.Visibility      = Visibility.Visible;
                progressBar.IsIndeterminate = true;

                currentAddress = address;
                // 显示新的地址
                UriBuilder uriBuilder = new UriBuilder(currentAddress);
                uriBuilder.UserName = string.Empty;
                uriBuilder.Password = string.Empty;
                addressBox.Text     = uriBuilder.Uri.ToString();


                // 不是ftp协议则抛出异常
                if (address.Scheme != "ftp" && address.Scheme != "ftps")
                {
                    throw new InvalidOperationException("只支持ftp协议");
                }

                // Host改变时重新连接
                if (client == null)
                {
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }
                else if (client.Host != address.Host ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.None && address.Scheme == "ftp") ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.Explicit && address.Scheme == "ftps") ||
                         (client.Port != 21 && address.Port < 0) ||
                         (client.Port != address.Port && address.Port >= 0) ||
                         !string.IsNullOrEmpty(address.UserInfo))
                {
                    client.Disconnect();
                    client.Dispose();
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }

                // FTP路径可能包含#号,#号后面的内容会被归入Fragment。
                string remotePath = address.LocalPath + address.Fragment;

                listItemsVM.Clear();
                foreach (var item in (await client.GetListingAsync(remotePath)).OrderBy(x => x.Name))
                {
                    listItemsVM.Add(await FtpListItemViewModel.FromFtpListItemAsync(item));
                }

                using (Data.HistoryContext db = new Data.HistoryContext())
                {
                    Data.HistoryEntry h = new Data.HistoryEntry
                    {
                        Time = DateTimeOffset.Now,
                        Url  = uriBuilder.Uri.ToString()
                    };
                    await db.AddAsync(h);

                    await db.SaveChangesAsync();
                }

                return(true);
            }
            catch (FluentFTP.FtpCommandException exception)
            {
                if (exception.CompletionCode == "530")
                {
                    errorMessage.Text = "认证失败,请尝试登录。";
                    loginButton.Flyout.ShowAt(loginButton);
                    loginErrorMessage.Visibility = Visibility.Visible;
                }
                else
                {
                    errorMessage.Text = string.Format(
                        "发生错误,FTP返回代码:{0}。详细信息:\n{1}", exception.CompletionCode, exception.Message);
                }

                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            catch (Exception exception)
            {
                errorMessage.Text       = string.Format("发生错误。详细信息:\n{0}", exception.Message);
                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            finally
            {
                progressBar.Visibility = Visibility.Collapsed;
                ftpSemaphore.Release();
            }
        }
Beispiel #6
0
 private void Button2_Click(object sender, EventArgs e)
 {
     FtpConnection.Disconnect();
     ServerFilesTree.Nodes.Clear();
 }
        public void SendCmd(string arg, bool user = false)
        {
            if (user)
            {
                log.Add(arg);
            }

            List <string> args    = arg.Split(' ').ToList();
            string        command = args[0];

            string GetArgs(int index, int to = -1)
            {
                string data = "";

                if (to == -1)
                {
                    to = 999999999;
                }
                for (int i = 0; i < args.Count; i++)
                {
                    if (i >= index && i <= to)
                    {
                        data += args[i] + " ";
                    }
                }
                return(data.TrimEnd());
            }

            if (arg.Split(' ')[0] == "echocl")
            {
                string color = arg.Split(' ')[1];
                Add(GetArgs(2), Color.FromName(color));
                return;
            }
            if (arg.Split(' ')[0] == "echoclcode")
            {
                try
                {
                    int R = Convert.ToInt32(arg.Split(' ')[1]);
                    int G = Convert.ToInt32(arg.Split(' ')[2]);
                    int B = Convert.ToInt32(arg.Split(' ')[3]);
                    Add(GetArgs(4), Color.FromArgb(R, G, B));
                }
                catch (Exception ex) { ResultsList.FromException(ex); }
                return;
            }
            if (arg.Split(' ')[0] == "echo")
            {
                Add(GetArgs(1));
                return;
            }
            if (arg.Split(' ')[0] == "ln")
            {
                if (GetArgs(1) == password)
                {
                    Add("Logged In!", Color.Lime);
                    login = true;
                    return;
                }
                if (GetArgs(1) == "logout")
                {
                    Add("Logged Out!", Color.Red);
                    login = false;
                    return;
                }
                Add("Bad Password!", Color.Red);
                return;
            }

            Add(" - " + arg, Color.Yellow);

            if (arg.Split(' ')[0] == "cd")
            {
                bool inoe = false;
                try
                {
                    var tmp = arg.Split(' ')[1];
                    if (tmp == " ")
                    {
                        inoe = true;
                    }
                }
                catch { inoe = true; }
                if (inoe)
                {
                    List <FluentFTP.FtpListItem> items = FTPHandle.GetItemsList("/ScreenShot");
                    Add("Items In Dir : Main : ");
                    for (int i = 0; i < items.Count; i++)
                    {
                        Add(items[i].Name, Color.Violet);
                    }
                }
                else
                {
                    List <FluentFTP.FtpListItem> items = FTPHandle.GetItemsList("/ScreenShot/" + arg.Split(' ')[1]);
                    Add("Items In Dir : " + arg.Split(' ')[1] + " : ");
                    for (int i = 0; i < items.Count; i++)
                    {
                        Add(items[i].Name, Color.Violet);
                    }
                }
                return;
            }
            if (arg.Split(' ')[0] == "wshmax")
            {
                if (login)
                {
                    try
                    {
                        wikipediamax = Convert.ToInt32(arg.Split(' ')[1]);
                        Add("Limit set to " + wikipediamax.ToString(), Color.Lime);
                    }
                    catch
                    {
                        return;
                    }

                    return;
                }
            }
            if (arg.Split(' ')[0] == "color")
            {
                try
                {
                    if (arg.Split(' ')[1] == "1" || arg.Split(' ')[1] == "0")
                    {
                        if (arg.Split(' ')[1] == "1")
                        {
                            color = true;
                        }
                        if (arg.Split(' ')[1] == "0")
                        {
                            color = false;
                        }
                        Add("Color is " + color.ToString(), Color.Lime);
                    }
                    richTextBox1.Visible = color;
                    richTextBox2.Visible = !color;
                    richTextBox2.Text    = richTextBox1.Text;
                    richTextBox2.SelectAll();
                    richTextBox2.SelectionColor = Color.Gray;
                    richTextBox2.DeselectAll();
                }
                catch
                {
                    return;
                }

                return;
            }
            if (arg.Split(' ')[0] == "wsearch" || arg.Split(' ')[0] == "wsh")
            {
                if (login)
                {
                    string querry = GetArgs(1);
                    try
                    {
                        Add("Searching : " + querry, Color.Aqua);
                        List <Search> searches  = new List <Search>();
                        Wikipedia     wikipedia = new Wikipedia(WikipediaNet.Enums.Language.Polish);
                        wikipedia.Limit = wikipediamax;
                        QueryResult   result  = wikipedia.Search(querry);
                        List <string> results = new List <string>();
                        foreach (Search s in result.Search)
                        {
                            searches.Add(s);
                            results.Add(s.Snippet);
                        }

                        for (int i = 0; i < results.Count; i++)
                        {
                            results[i] = results[i].Replace("<span class=\"searchmatch\">", "");
                            results[i] = results[i].Replace("</span>", "");
                        }
                        for (int i = 0; i < results.Count; i++)
                        {
                            Add(results[i], Color.Violet);
                        }
                    }
                    catch (Exception ex) { Add(ex.ToString()); }
                    return;
                }
            }
            if (arg.Split(' ')[0] == "clear" || arg.Split(' ')[0] == "cs")
            {
                richTextBox1.Text = "";
                log.Clear();
                return;
            }
            if (arg.Split(' ')[0] == "credentials")
            {
                if (args.Count > 1)
                {
                    if (args[1] == "reset")
                    {
                        FTPHandle.credential.Password = FTPHandle.haslo;
                        FTPHandle.credential.UserName = FTPHandle.login;
                        FTPHandle.adres = FTPHandle.adresfactory;
                        Add("Credentials changed to factory", Color.Violet);
                    }
                    if (args[1] == "ping")
                    {
                        try
                        {
                            FluentFTP.FtpClient f = new FluentFTP.FtpClient(FTPHandle.adres, FTPHandle.credential);
                            f.Connect();
                            f.Disconnect();
                            Add("Credentials ping succes", Color.Lime);
                        }
                        catch (Exception ex) { ResultsList.FromException(ex); Add("To reset write : credentials reset", Color.Violet); }
                        return;
                    }
                    if (args[1] == "changepass")
                    {
                        FTPHandle.credential.Password = GetArgs(2);
                        Add("Password changed to : " + GetArgs(2), Color.Violet);
                    }
                    if (args[1] == "changelogin")
                    {
                        FTPHandle.credential.UserName = GetArgs(2);
                        Add("Login changed to        : " + GetArgs(2), Color.Violet);
                    }
                    if (args[1] == "changehost")
                    {
                        FTPHandle.adres = GetArgs(2);
                        Add("Host changed to         : " + GetArgs(2), Color.Violet);
                    }
                }
                Add("Login Credentials", Color.Yellow);
                Add("Host         : " + FTPHandle.adres, Color.Violet);
                Add("Login        : "******"Password : "******"";
                for (int i = 0; i < FTPHandle.credential.Password.Length; i++)
                {
                    passhide += "*";
                }
                if (!login)
                {
                    Add("Password : "******"Null Command", Color.Red);
        }
Beispiel #8
0
 public void Disconnect()
 {
     _client.Disconnect();
 }
 public override void Disconnect()
 {
     _client.Disconnect();
     OnPropertyChanged("IsConnected");
 }
Beispiel #10
0
 public void Dispose()
 {
     Client.Disconnect();
 }
Beispiel #11
0
 public void Close()
 {
     ftp?.Disconnect();
     ftp = null;
 }