2017년 3월 2일 목요일

[Development story] GFtp - #1

I am studying C# since a week ago.
People say "C# is easy to learn." But I don't.
Because I have been programming with only C++ about 20 years.
So It's a little difficult to learn C#.

Now, I am studying C# with "C# 6.0 완벽가이드" and I almost read the book.
So I will try to develop FTP program and that development story will be writen by me.

Today I write first day story.
I am C# beginner. So there will be an inexperienced part.
Welcome any replys, opinions.

1. Program introduce

 - Very very simple FTP program.

2. Functions

 - Translate local file to FTP.

3. Demo




4. Code Snippets

 - Display all files in the current directory.

        // Refresh FileTreeView
        // Display all file in the current directory.
        void RefreshFileTreeViewOfCurrentDirectory()
        {
            // initialize file tree view control
            fileTreeView.Nodes.Clear();

            // Get all file list  directory list of my pc;
            DirectoryInfo di = new DirectoryInfo(_currentDirectory);

            // Get all sub directory
            FileInfo[] fiArray = di.GetFiles("*.*");

            // root node
            TreeNode rootNode = new TreeNode(_currentDirectory);
            rootNode.Text = _currentDirectory;

            foreach(FileInfo f in fiArray)
            {
                TreeNode fileNode = new TreeNode(f.Name);
                rootNode.Nodes.Add(fileNode);
            }
         
            fileTreeView.Nodes.Add(rootNode);

            // expand all file tree view
            fileTreeView.ExpandAll();
        }


 - Select current directory using FolderBrowserDialog

        // When the select director button clicked, select current directory and then refresh file tree view
        private void selectDirectoyButton_Click(object sender, EventArgs e)
        {
            // select directory
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.SelectedPath = _currentDirectory;
            if (dlg.ShowDialog() == DialogResult.Cancel)
                return;
            _currentDirectory = dlg.SelectedPath;

            RefreshFileTreeViewOfCurrentDirectory();
        }

-  Refresh ftp file list box 

        // Refresh ftp file list box
        void RefreshFtpFileListBox(string ftpAddr, string id, string password)
        {
            string[] fileList = GetAllFileListFromFtp(ftpAddr, id, password);
         
            // display file list into list view
            foreach (string filename in fileList)
            {
                ftpFileListBox.Items.Add(filename);
            }
        }

- Get all files on ftp root directory.

        // Get all files on ftp root directory
        private string[] GetAllFileListFromFtp(string ftpAddr, string id, string password)
        {
            Uri ftpUri = new Uri(ftpAddr);
            FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(ftpUri);
            ftpReq.Credentials = new NetworkCredential(id, password);
            ftpReq.Timeout = 30000;
            ftpReq.Method = WebRequestMethods.Ftp.ListDirectory;

            FtpWebResponse ftpRes = (FtpWebResponse)ftpReq.GetResponse();

            StreamReader reader = new StreamReader(ftpRes.GetResponseStream(), System.Text.Encoding.Default);

            string str = reader.ReadToEnd();
            string[] files = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            ftpRes.Close();

            return files;
        }


-  Translate file from local to ftp or from ftp to local (upload or download)

        // Translate files from local to ftp or from ftp to local
        private bool TranslateFiles(string[] files, bool IsUpload)
        {
            // connect ftp
            WebClient wc = new WebClient { Proxy = null };
            wc.BaseAddress = _ftpAddress;
            if (_id != "" && _password != "")
            {
                wc.Credentials = new NetworkCredential(_id, _password);
            }

            try
            {
                foreach (string filename in files)
                {
                    string pathName = Path.Combine(_currentDirectory, filename);
                    // upload
                    if (IsUpload)
                    {
                        wc.UploadFile(pathName, filename);
                    }
                    // download
                    else
                    {
                        wc.DownloadFile(filename, pathName);
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

5. Result

 I done for one day.
I planed develop very simple ftp. But it's too simple. GFtp can select only one file. And It has no image, so it's not pretty.
So I decided to do version up.
Next time, I will add some functions for more easy and more pretty GFtp.

This is GFtp ver1.0 and I share source file by link.

There are all source in the following link.
https://drive.google.com/drive/folders/0B7BKBqLU8mglMl9UNFZvWmFQUGs?usp=sharing

GFtp is developed using Visual Studio 2015.


댓글 없음:

댓글 쓰기