Esempio n. 1
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;
            }
        }