예제 #1
0
        public void GetRemoteDirectoryAndFile(List <ListInfo> listInfos)
        {
            var action = new Action(() =>
            {
                _remoteListView.Clear();

                //循环添加文件夹

                foreach (var dir in listInfos)
                {
                    ListViewItem item = new ListViewItem();

                    item.Text = dir.FileName;

                    item.SubItems.Add(dir.Type);

                    if (dir.Type == "文件夹")
                    {
                        item.ImageIndex = _sysIcons.SmallImageList.Images.IndexOfKey("FileFolder");

                        item.Tag = new DirectoryClass()
                        {
                            Name = dir.FileName
                        };
                    }
                    else
                    {
                        item.ImageIndex = _sysIcons.GetIconIndex(dir.FileName, false);

                        var ext = Path.GetExtension(dir.FileName);

                        string desc = string.Empty;

                        IconHelper.GetExtsDescription(ext, out desc);

                        item.Tag = new FileClass()
                        {
                            Name            = dir.FileName,
                            FileExtendsName = desc,
                            FileMax         = dir.Size / (1024 * 1024)
                        };
                    }
                    item.SubItems.Add("");
                    item.SubItems.Add(dir.Size.ToString());

                    _remoteListView.Items.Add(item);
                }

                FillRemoteDirectoryHeader();
            });

            if (_remoteListView.InvokeRequired)
            {
                _remoteListView.Invoke(action);
            }
            else
            {
                action.Invoke();
            }
        }
예제 #2
0
        /// <summary>
        /// 根据节点信息,得到文件夹和文件,把显示到ListView列表中
        /// </summary>
        /// <param name="info"></param>
        public void GetDirectoryAndFile(DirectoryClass info)
        {
            var action = new Action(() =>
            {
                _localListView.Clear();

                DirectoryInfo dirs = new DirectoryInfo(info.FullName);
                if (dirs.ToString() == @"C:\Documents and Settings\Administrator\桌面")
                {
                    return;
                }

                //循环添加文件夹

                foreach (DirectoryInfo dir in dirs.GetDirectories())
                {
                    if (dir.Name == "$RECYCLE.BIN" || dir.Name == "System Volume Information")
                    {
                        continue;
                    }

                    #region 如果文件是只读的就不要读出来,不然会报错
                    string[] fileAttrites = File.GetAttributes(dir.FullName).ToString().Split(',');
                    //如果属性大于0的就说明有问题,只要属性里面有hidden就不要显示出来

                    bool isHideen = false; //是不是隐藏文件

                    foreach (string cAttrites in fileAttrites)
                    {
                        if (cAttrites.Equals("hidden", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isHideen = true;
                            break;
                        }
                    }
                    #endregion

                    #region 判断isHideen=false,这样就可以添加资料到ListView中去

                    if (!isHideen)  //文件夹
                    {
                        DirectoryClass dc = new DirectoryClass();
                        dc.Name           = dir.Name;
                        dc.FullName       = dir.FullName;
                        dc.IsDirectory    = true;
                        dc.IsFixDriver    = false;
                        dc.CreateTime1    = dir.LastWriteTime.ToString("yyyy/MM/dd HH:mm");

                        ListViewItem item = new ListViewItem();
                        item.Text         = dc.Name;
                        item.Tag          = dc;
                        item.ImageIndex   = _sysIcons.SmallImageList.Images.IndexOfKey("FileFolder");

                        item.SubItems.Add("文件夹");
                        item.SubItems.Add(dc.CreateTime1);
                        item.SubItems.Add("");

                        _localListView.Items.Add(item);
                    }

                    #endregion
                }

                //循环添加文件
                foreach (FileInfo file in dirs.GetFiles())
                {
                    #region 如果文件是只读的就不要读出来,不然会报错
                    string[] fileAttrites = File.GetAttributes(file.FullName).ToString().Split(',');
                    //如果属性大于0的就说明有问题,只要属性里面有hidden就不要显示出来

                    bool isHideen = false; //是不是隐藏文件

                    foreach (string cAttrites in fileAttrites)
                    {
                        if (cAttrites.Equals("hidden", StringComparison.InvariantCultureIgnoreCase) || cAttrites.Equals("readonly", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isHideen = true;
                            break;
                        }
                    }
                    #endregion

                    #region 如果isHideen==false就显示文件

                    if (!isHideen)
                    {
                        FileClass newFile   = new FileClass();
                        newFile.Name        = file.Name;
                        newFile.FullName    = file.FullName;
                        newFile.FileExtends = file.Extension;  //扩展名
                        newFile.CreateTime1 = file.LastWriteTime.ToString("yyyy/MM/dd HH:mm");


                        newFile.FileMax = Math.Round((file.Length / 1024.0 / 1024.0), 2);

                        newFile.IsDirectory = false;

                        //文件的扩展名称叫什么
                        string desc;

                        IconHelper.GetExtsDescription(newFile.FileExtends, out desc);

                        newFile.FileExtendsName = desc;


                        ListViewItem item = new ListViewItem();
                        item.Text         = newFile.Name;
                        item.Tag          = newFile;
                        item.ImageIndex   = _sysIcons.GetIconIndex(newFile.FullName, false);

                        //子项
                        item.SubItems.Add(newFile.FileExtendsName);
                        item.SubItems.Add(newFile.CreateTime1);

                        double len = newFile.FileMax;
                        if (len >= 1000)
                        {
                            len = Math.Round((len / 1024.0), 2);

                            item.SubItems.Add(len + "GB");
                        }
                        else
                        {
                            item.SubItems.Add(newFile.FileMax + "MB");
                        }

                        _localListView.Items.Add(item);
                    }
                    #endregion
                }

                FillDirectoryHeader();
            });

            if (_localListView.InvokeRequired)
            {
                _localListView.Invoke(action);
            }
            else
            {
                action.Invoke();
            }
        }