Esempio n. 1
0
        // Sự kiện form_load
        private void Main_FTP_Load(object sender, EventArgs e)
        {
            // bỏ qua check crossthread khi gọi thread truyền main_load ở mục xóa tập tin/ thư mục
            CheckForIllegalCrossThreadCalls = false;
            timer1.Start();
            timer1.Enabled = true;
            SpeedTest st = new SpeedTest();

            lblSpeed.Text = "Tốc độ mạng hiện tại : " + st.CheckInternetSpeed() + "Kb/s";
            btnBackToHome.Hide(); // ẩn đi nút quay về trang chính
            lstFTP.Items.Clear(); // xóa hết toàn bộ dữ liệu trong listview
            lblDownload.Hide();   // ẩn đi thông báo download
            // using ở đây để khai báo Namespace của thư viện Ftp.dll sử dụng
            using (Ftp client = new Ftp())
            {
                // kết nối với Server ip với các giá trị bên form login truyền qua
                client.Connect(_ip, _port);
                client.Login(_username, _password);

                /* Khai báo 1 list danh sách các tập tin hiện có trên server
                 *   thông qua phương thức getList() */
                List <FtpItem> items = client.GetList();

                // duyệt qua các đối tượng FtpItem có trong list
                // rồi in ra trong ListView
                foreach (FtpItem item in items)
                {
                    // thêm từng tập tin vào listview
                    ListViewItem ds = new ListViewItem(item.Name);
                    ds.SubItems.Add(item.ModifyDate.ToShortDateString());
                    ds.SubItems.Add(GetFileSize(Convert.ToInt64(item.Size)));
                    if (item.IsFile == true)
                    {
                        ds.SubItems.Add("Là tập tin");
                    }
                    if (item.IsFolder == true)
                    {
                        ds.SubItems.Add("Là thư mục");
                    }

                    try
                    {
                        string filename   = item.Name;           // lấy toàn bộ tên file ( tên + phần mở rộng)
                        var    split      = filename.Split('.'); // thực thi phương thức split cắt ra ở dấu . đuôi mở rộng
                        string duoiMoRong = split[1];            // lấy phần mở rộng để gán cột thông tin
                        ds.SubItems.Add(duoiMoRong + " file ");
                    }
                    catch (IndexOutOfRangeException)
                    {
                        // nếu không phải là file thì là thư mục
                        ds.SubItems.Add("Thư mục");
                    }


                    lstFTP.Items.Add(ds);
                }

                client.Close();
            }
        }
Esempio n. 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            // khi nhấn nút rename
            string newName = txtNewName.Text;

            using (Ftp client = new Ftp())
            {
                // kết nối đến server FTP
                client.Connect(_ip, _port);
                client.Login(_username, _password);

                // nếu là tập tin
                if (_type == "Là tập tin")
                {
                    // lấy tên tập tin
                    string oldName    = txtOldName.Text;
                    var    name       = oldName.Split('.');             // thực thi phương thức split cắt ra ở dấu . đuôi mở rộng
                    string duoiMoRong = name[1];                        // lấy phần mở rộng để lát gán lại ở giá trị mới

                    client.Rename(oldName, newName + "." + duoiMoRong); // thực thi phương thức rename
                }
                // nếu là thư mục thì đổi tên thẳng không cần split
                else if (_type == "Là thư mục")
                {
                    client.Rename(txtOldName.Text, newName);
                }
                // in câu thông báo hoàn tất
                MessageBox.Show("Đổi tên hoàn tất.");
                txtOldName.Text = "";
                txtNewName.Text = "";
                client.Close();
            }
        }
        // phương thức upload data
        private void Upload_data()
        {
            // using ở đây để khai báo Namespace của thư viện Ftp.dll sử dụng
            using (Ftp client = new Ftp())
            {
                client.Connect(_ip, _port);                    // thực thi phương thức connect(Ip, port, false/true ( nếu Server có SSL thì là true, còn không có thì mặc định sẽ là False)
                client.Login(_username, _password);            // username login vào Sever thông qua phương thức Login(username, password)

                client.Upload(txtFileName.Text, txtPath.Text); // thực thi phương thức upload( tên file cần upload, đường dẫn tuyệt đối tới file đó)

                client.Close();
            }
        }
Esempio n. 4
0
        // Phương thức download file truyền vào đối tượng client và tên tập tin muốn download
        private void DownloadFile(Ftp client, string filename, string path)
        {
            // Bỏ qua kiểm tra crossthread
            CheckForIllegalCrossThreadCalls = false;
            client.Connect(_ip, _port); // kết nối đến server FTP
            client.Login(_username, _password);

            // thực thi phương thức download(tên tập tin muốn download, vị trí lưu)
            // vị trí lưu ở đây là folder được chọn từ user.
            client.Download(filename, path + "\\" + filename); // thực thi phương thức download(tên file muốn download, vị trí lưu)


            client.Close();
        }
Esempio n. 5
0
        // sự kiện nhấn nút xóa file hoặc folder trong thanh menu bar
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa khong?", "XÓA ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                using (Ftp client = new Ftp())
                {
                    try
                    {
                        // kết nối đến server FTP
                        client.Connect(_ip, _port);
                        client.Login(_username, _password);

                        // lấy giá trị tên file hoặc tập tin được chọn ở listview
                        string filename = lstFTP.SelectedItems[0].Text;
                        // nếu giá trị này là tập tin.
                        if (lstFTP.SelectedItems[0].SubItems[3].Text == "Là tập tin")
                        {
                            // thực thi phương thức DeleteFile(tên tập tin muốn xóa)
                            client.DeleteFile(filename);
                            // sau đó gọi lại hàm main_load để cập nhật lại danh sách file
                            Thread t = new Thread(() => button4_Click(sender, e));
                            t.IsBackground = true;
                            t.Start();
                        }
                        // nếu giá trị này là thư mục
                        else if (lstFTP.SelectedItems[0].SubItems[3].Text == "Là thư mục")
                        {
                            // thực thi phương thức DeleteFolder(tên thư mục muốn xóa)
                            client.DeleteFolder(filename);
                            // sau đó gọi lại hàm main_load để cập nhật lại danh sách file
                            Thread t = new Thread(() => Main_FTP_Load(sender, e));
                            t.IsBackground = true;
                            t.Start();
                        }
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        // nếu không chọn gì cả thì hiện câu thông báo lỗi
                        MessageBox.Show("Phải chọn tập tin để xóa");
                    }
                }
            }
            else
            {
                return;
            }
        }
Esempio n. 6
0
        // khi nhấn nút rename
        private void renameSelectedFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa khong?", "XÓA ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                using (Ftp client = new Ftp())
                {
                    try
                    {
                        // kết nối đến Server FTP
                        client.Connect(_ip, _port);
                        client.Login(_username, _password);

                        // lấy tên file được chọn trong listview
                        string filename = lstFTP.SelectedItems[0].Text;
                        // kiểu file là tập tin hay thư mục
                        string type = lstFTP.SelectedItems[0].SubItems[3].Text;

                        // sau đó truyền các dữ liệu này sang form rename.
                        if (lstFTP.SelectedItems[0].SubItems[3].Text == "Là tập tin")
                        {
                            this.Hide();
                            Rename r = new Rename(_ip, _port, _username, _password, type, filename);
                            r.Show();
                        }
                        else if (lstFTP.SelectedItems[0].SubItems[3].Text == "Là thư mục")
                        {
                            this.Hide();
                            Rename r = new Rename(_ip, _port, _username, _password, type, filename);
                            r.Show();
                        }
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        MessageBox.Show("Phải chọn tập tin để đối tên");
                    }
                }
            }
            else
            {
                return;
            }
        }
Esempio n. 7
0
        // sự kiện nhấn 2 lần vào item trong listview
        private void lstFTP_ItemActivate(object sender, EventArgs e)
        {
            // khi đã vào thư mục con thì hiện nút quay trở lại trang chính
            btnBackToHome.Show();
            String text = lstFTP.SelectedItems[0].Text; // lấy giá trị double click item đó

            using (Ftp client = new Ftp())
            {
                // kết nối đến server FTP
                client.Connect(_ip, _port);
                client.Login(_username, _password);

                // tạo 1 thread thực thi phương thức openFolder
                Thread t = new Thread(() => openFolder(client, text));
                t.IsBackground = true;
                t.Start();

                t.Join();
            }
        }
Esempio n. 8
0
        // sự kiện nhấn nút tạo mới thư mục
        private void button1_Click(object sender, EventArgs e)
        {
            // kiểm tra nếu bỏ trống thì thông báo lỗi
            if (string.IsNullOrEmpty(txtFolder.Text))
            {
                MessageBox.Show("Không được bỏ trống!");
            }
            string folder = txtFolder.Text; // lấy tên thư mục mới

            using (Ftp client = new Ftp())
            {
                // kết nối đến server FTP
                client.Connect(_ip, _port);
                client.Login(_username, _password);

                // thực thi phương thức tạo mới thư mục CreateFolder(tên thư mục tạo mới)
                client.CreateFolder(folder);

                // in câu thông báo
                MessageBox.Show("Đã tạo thư mục tên: " + folder);

                client.Close();
            }
        }
Esempio n. 9
0
        private void btnKetNoi_Click(object sender, EventArgs e)
        {
            try
            {
                string ip       = txtIP.Text;              // Lấy giá trị từ txtIP
                string password = txtPassword.Text;        // Lấy giá trị từ txtPassword
                string username = txtUsername.Text;        // Lấy giá trị từ txtUsername
                int    port     = int.Parse(txtPort.Text); // Lấy giá trị từ txtPort

                /* Xét giá trị port nhập vào, nếu bé hơn 0 hoặc lớn hơn 65535 thì
                 *   thông báo lỗi */
                if (port <= 0 || port >= 65535)
                {
                    MessageBox.Show("Port phải lớn hơn 0 và bé hơn 65535");
                    return;
                }

                // using ở đây để khai báo Namespace của thư viện Ftp.dll sử dụng
                using (Ftp client = new Ftp())            // Tạo mới 1 đối tượng Ftp tên client, để sử dụng các phương thức có trong thư viện Ftp
                {
                    try                                   // thực hiện try catch để bắt các exception như sai mật khẩu/ username/ sai IP
                    {
                        client.Connect(ip, port);         // thực thi phương thức connect(Ip, port, false/true ( nếu Server có SSL thì là true, còn không có thì mặc định sẽ là False)
                        client.Login(username, password); // username login vào Sever thông qua phương thức Login(username, password)

                        // nếu như đăng nhập thành công, thì đối tượng client sẽ không bằng null
                        if (client == null)
                        {
                            // khi đăng nhập không thành công thì thông báo lỗi
                            MessageBox.Show("Error");
                            return;
                        }
                        else
                        {
                            // đăng nhập thành công, thông báo cho người dùng biết là thành công
                            MessageBox.Show("Success");
                            this.Hide(); // tắt cửa sổ đăng nhập

                            /* Hiện form giao diện chính khi đăng nhập thành công, và có đính kèm
                             *  các giá trị username, password, ip, port để xử lý về sau.*/
                            Main_FTP m = new Main_FTP(txtUsername.Text, txtIP.Text, int.Parse(txtPort.Text), txtPassword.Text);
                            m.Show();
                        }
                    }
                    // bắt ngoại lệ khi không đúng usernam, password hay ip
                    catch (FtpException)
                    {
                        MessageBox.Show("Sai tên đăng nhập hoặc mật khẩu \n Hoặc sai IP");
                    }
                }
            }
            // bắt ngoại lệ khi người dùng không nhập liệu
            catch (ArgumentException)
            {
                MessageBox.Show("Không được để trống");
            }
            // bắt ngoại lệ khi người dùng nhập port không phải là số
            catch (FormatException)
            {
                MessageBox.Show("Port phải là số");
            }
        }