2017년 3월 5일 일요일

[Development Story] GFtp - #2

GFtp's first update.
GFtp will be made more pretty, more easy.
So I am going to update GFtp with following list. This update is GFtp v1.1.

1. Update list

 - Image buttons
 - multi select.

2. Searching icons for buttons

 - http://www.flaticon.com/

3. Screenshot

 - GFtp v1.1 is became more pretty than GFtp v1.0.
   But I think that it is too far.

4. Code snippets


- To display file list, I changed a TreeView control to DataGridView. 

   Following codes is to display file list into DataGridView.
   GridFileInfo is a class for file list.
   Function named DisplayFilesToGridView(...) is to display file infoes into DataGridView.
   Caution, to select full columns on DataGridView is set "DataGridView.Columns[col].SortMode = DataGridViewColumnSortMode.NotSortable"

       public class GridFileInfo
       {
           public string Name { get; set; }
           public long Size { get; set; }
       }

        // Display file to grid view
        void DisplayFilesToGridView(DataGridView gridView, GridFileInfo[] fileinfos)
        {
            // initialize file tree view control
            gridView.RowCount = fileinfos.Length;
            gridView.ColumnCount = 2;

            // set column's width
            gridView.Columns[0].Width = 200;
            gridView.Columns[1].Width = 150;

            // set header titles
            gridView.Columns[0].HeaderText = "Name";
            gridView.Columns[1].HeaderText = "Size";

            // Change column's sortmode to NotSortable for DataGridViewSelectionMode.FullColumnSelect
            for (int i = 0; i < gridView.ColumnCount; ++i)
            {
                gridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            gridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            // Set files to grid view
            int rowIndex = 0;
            foreach (GridFileInfo f in fileinfos)
            {
                gridView.Rows[rowIndex].Height = 24;
                gridView.Rows[rowIndex].Cells[0].Value = f?.Name;
                gridView.Rows[rowIndex].Cells[1].Value = f?.Size;

                rowIndex++;
            }
        }

- Get selected file names on DataGridView

  First column contains file name. So you need to get file names on DataGridView, you have to first columns's text.
  Following codes is to get file names on DataGridView.

        // Get file names selected in the grid view
        private string[] GetSelectedFileOfGridView(DataGridView gridView)
        {
            string[] files = new string[gridView.SelectedRows.Count];
            int idx = 0;
            foreach (DataGridViewRow row in gridView.SelectedRows)
            {
                files[idx++] = row.Cells[0].Value.ToString();
            }

            return files;
        }

  GetSelectedFileOfGridView function is used for both current directory DataGridView and Ftp DataGridView.

  - Get file informations on Ftp using FtpWebRequest and FtpWebResponse.

    I changed Request method from WebRequestMethods.Ftp.ListDirectory to WebRequestMethods.Ftp.ListDirectoryDetails.
    WebRequestMethods.Ftp.ListDirectoryDetails allows you get file information more detail like file name, file attribute, file size etc.

     Following codes is to get file detail informations on Ftp.

        // Get all files on ftp root directory
        private GridFileInfo[] GetAllGridFileInfosFromFtp(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; // 30 seconds timeout
            ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

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

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

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


            GridFileInfo[] gridFileInfos = new GridFileInfo[fileInfos.Length];
            int idx = 0;
            foreach(string infoString in fileInfos)
            {
                string[] infoArray = infoString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                gridFileInfos[idx] = new GridFileInfo();
                gridFileInfos[idx].Size = long.Parse(infoArray[4]);
                gridFileInfos[idx].Name = infoArray[8];
                idx++;
            }

            ftpRes.Close();
            return gridFileInfos;
        }

5. Demo


Next time I will update this program to that Ftp Data Grid View is more comfortable.
Source code is shared to anybody.
https://drive.google.com/drive/u/0/folders/0B7BKBqLU8mglMl9UNFZvWmFQUGs
 

댓글 없음:

댓글 쓰기