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
        // phương thức mở thư mục con truyền vào đối tượng client và tên thư mục
        private void openFolder(Ftp client, string text)
        {
            // bỏ qua kiểm tra crossthread
            CheckForIllegalCrossThreadCalls = false;
            try
            {
                // thực thi phương thức getList(truyền vào tên thư mục)
                // phương thức này sẽ return lại ở dạng là List
                List <FtpItem> file_in_folder = client.GetList(text);

                // duyệt qua từng đối tượng ftpitem và in ra trong listview
                foreach (FtpItem item in file_in_folder)
                {
                    // thêm từng tập tin vào listview
                    lstFTP.Items.Clear();
                    ListViewItem ds = new ListViewItem(item.Name);
                    ds.SubItems.Add(item.ModifyDate.ToShortDateString());
                    ds.SubItems.Add(item.Size.ToString());

                    lstFTP.Items.Add(ds);
                }
            }
            catch (FtpResponseException)
            {
                // nếu thư mục được chọn rỗng thì in ra câu thông báo!
                MessageBox.Show("Thư mục trống !!");
            }
        }