Esempio n. 1
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();
            }
        }
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 !!");
            }
        }
Esempio n. 3
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();
            }
        }
        // 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. 5
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. 6
0
        // nhấn nút download ( đã fix lỗi access denied)
        private void button2_Click(object sender, EventArgs e)
        {
            string path = "";

            try
            {
                // tạo 1 biến string tên filename lấy tên file được chọn.
                string filename = lstFTP.SelectedItems[0].Text; // chọn file trong listbox

                // mở hộp thoại để chọn đường dẫn để tải tập tin
                using (var folderDialog = new FolderBrowserDialog())
                {
                    if (folderDialog.ShowDialog() == DialogResult.OK)
                    {
                        path = folderDialog.SelectedPath; // lưu đường dẫn vào biến path
                    }
                    else if (folderDialog.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                }

                // xử lý download

                // using ở đây để khai báo Namespace của thư viện Ftp.dll sử dụng
                using (Ftp client = new Ftp())
                {
                    // Tạo thread t1 thực hiện phương thức Download file
                    Thread t1 = new Thread(() => DownloadFile(client, filename, path));
                    t1.IsBackground = true;
                    t1.Start();

                    // Tạo thread t2 thực hiện việc hiện thông báo đang download
                    Thread t2 = new Thread(() => HienThongBao("ĐANG DOWNLOAD"));
                    t2.Start();

                    // khi download hoàn tất thì stop 2 thread lại
                    t1.Join();
                    t2.Abort();

                    // In câu thông báo
                    MessageBox.Show("DOWNLOAD HOÀN TẤT!!!");
                    AnThongBao(); // Ẩn đi dòng thông báo đang download
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("Phải chọn 1 file để download.");
            }
        }
Esempio n. 7
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. 8
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. 9
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. 10
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. 11
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ố");
            }
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            //Environment.SetEnvironmentVariable( "working-folder", @"C:\rpDocuments\Programs\MR\ParadePrototype\FrankieLee" );
            string workingFolder = Environment.GetEnvironmentVariable( "workingfolder", EnvironmentVariableTarget.Process );

            CommandLineArgs.CmdLine cl = new CommandLineArgs.CmdLine();

            //args = new string[] { "-method", "download", "-pcfilename", @"C:\rpDocuments\Programs\MR\ParadePrototype\FrankieLee\testone.rpg", "-member", "hellorpg" };
            //args = new string[] { "-method", "download", "-pcfilename", "roger.rpg", "-member", "hellorpg" };
            //args = new string[] { "--help" };

            cl.AddHelpLine("FTP source member upload or download.");
            cl.AddHelpLine("Set enviroment variable 'workingfolder' so you");
            cl.AddHelpLine("can use just the filename portion of pcFilename.");

            cl.AddRequiredArgumentDefinition("-method", "get(upload) or put(download).");
            cl.AddRequiredArgumentDefinition("-pcFilename", "Fully qualified PC file name.");
            cl.AddRequiredArgumentDefinition("-member", "IBM i member name.");
            cl.AddOptionalArgumentDefinition("-server", "Server name or IP address.");
            cl.AddOptionalArgumentDefinition("-library", "IBM i library name.");
            cl.AddOptionalArgumentDefinition("-sourceFile", "IBM i source physical file.");
            cl.AddOptionalArgumentDefinition("-user", "IBM i user name.");
            cl.AddOptionalArgumentDefinition("-password", "IBM i user password.");

            cl.Initialize(args);

            string pcFilename = cl.GetArgumentValue("-pcFileName");

            if (!String.IsNullOrEmpty(workingFolder)) {
                workingFolder = (workingFolder.EndsWith(@"\")) ? workingFolder : workingFolder + @"\";
                pcFilename = workingFolder + pcFilename;
            }

            Ftp ftp = new Ftp() {server = cl.GetArgumentValue("-server", "Cypress"),
                                 library = cl.GetArgumentValue("-library", "rpmobile"),
                                 sourceFile = cl.GetArgumentValue("-sourceFile", "qrpglesrc"),
                                 user = cl.GetArgumentValue("-user", "rogerso"),
                                 password = cl.GetArgumentValue("-password", "ASNA")};

            string result = "";
            string member = cl.GetArgumentValue( "-member" );

            try {
                if (cl.GetArgumentValue("-method").ToLower() == "put" || cl.GetArgumentValue("-method").ToLower() == "upload" )  {
                    result = ftp.Put( pcFilename, member );
                    Console.WriteLine("Uploaded {0}\r\n to {1}/{2}/{3}/{4}.", pcFilename, ftp.server, ftp.library, ftp.sourceFile, member);
                }
                else if ( cl.GetArgumentValue( "-method" ).ToLower() == "get" || cl.GetArgumentValue( "-method" ).ToLower() == "download") {
                    result = ftp.Get( pcFilename, member );
                    Console.WriteLine( "Downloaded {0}\r\n from {1}/{2}/{3}/{4}.", pcFilename, ftp.server, ftp.library, ftp.sourceFile, member );
                }
                else {
                    throw new Exception( "-method must be be 'get', 'download',  'put', or 'upload'." );
                }
            }
            catch ( System.Exception e ) {
                Console.WriteLine("*ERROR* Program failure...");
                Console.WriteLine(e.Message);
                Console.WriteLine("Note: 'error (426)' probably means the IBM member doesn't exist.");
            }

            Console.WriteLine(result);
            //Console.WriteLine("Press any key to continue...");
            //Console.ReadKey();
        }