コード例 #1
0
        private void ShowSearchResult()
        {
            lock_Form = true;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            //开始数据更新
            AllFiles.BeginUpdate();
            AllFiles.Items.Clear();
            ilstIcons.Images.Clear();
            ilstIcons.Images.Add(Resources.folder);

            int fileCount = SDirs.Count() + SFlists.Count();

            //列出所有文件夹
            foreach (String[] dir in SDirs)
            {
                try
                {
                    if (dir[0].Length > 30)
                    {
                        continue;
                    }
                    if (dir[1].Length > 100)
                    {
                        continue;
                    }
                    if (!Directory.Exists(dir[1]))
                    {
                        continue;
                    }
                    DirectoryInfo dirInfo = new DirectoryInfo(dir[1]);
                    ListViewItem  item    = AllFiles.Items.Add(dirInfo.Name, 0);
                    item.Tag = dirInfo.FullName;
                    item.SubItems.Add(dirInfo.FullName);
                    try
                    {
                        item.SubItems.Add(dirInfo.LastWriteTime.ToString());
                    }
                    catch
                    {
                        item.SubItems.Add("----");
                    }

                    item.SubItems.Add("文件夹");
                    item.SubItems.Add("");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            //列出所有文件
            foreach (string[] file in SFlists)
            {
                try
                {
                    if (file[0].Length > 200)
                    {
                        continue;
                    }
                    if (file[1].Length > 400)
                    {
                        continue;
                    }
                    if (!File.Exists(file[1]))
                    {
                        continue;
                    }
                    FileInfo fileInfo = new FileInfo(file[1]);
                    if (fileInfo.Extension == ".lnk")
                    {
                        continue;
                    }
                    ListViewItem item = AllFiles.Items.Add(fileInfo.Name);
                    //为exe文件或无拓展名
                    if (fileInfo.Extension == ".exe" || fileInfo.Extension == "" || fileInfo.Extension == ".xml")
                    {
                        if (fileInfo.Extension == "")
                        {
                            ilstIcons.Images.Add(fileInfo.Name, Resources.fileIco);
                        }
                        else if (fileInfo.Extension == ".xml")
                        {
                            ilstIcons.Images.Add(fileInfo.Name, Resources.xml);
                        }
                        else
                        {
                            //通过当前系统获得文件相应图标
                            Icon fileIcon = GetSystemIcon.GetIconByFileName(fileInfo.FullName);
                            //因为不同的exe文件一般图标都不相同,所以不能按拓展名存取图标,应按文件名存取图标
                            ilstIcons.Images.Add(fileInfo.Name, fileIcon);
                        }
                        item.ImageKey = fileInfo.Name;
                    }
                    //其他文件
                    else
                    {
                        if (!ilstIcons.Images.ContainsKey(fileInfo.Extension))
                        {
                            Icon fileIcon = GetSystemIcon.GetIconByFileName(fileInfo.FullName);
                            if (fileIcon == null)
                            {
                                fileIcon = Resources.fileIco;
                            }
                            //因为类型(除了exe)相同的文件,图标相同,所以可以按拓展名存取图标
                            ilstIcons.Images.Add(fileInfo.Extension, fileIcon);
                        }

                        item.ImageKey = fileInfo.Extension;
                    }

                    item.Tag = fileInfo.FullName;
                    item.SubItems.Add(fileInfo.FullName);

                    try
                    {
                        item.SubItems.Add(fileInfo.LastWriteTime.ToString());
                    }
                    catch
                    {
                        item.SubItems.Add("----");
                    }

                    item.SubItems.Add(fileInfo.Extension + "文件");
                    try
                    {
                        item.SubItems.Add(ShowFileSize(fileInfo.Length).Split('(')[0]);
                    }
                    catch
                    {
                        item.SubItems.Add("0KB");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            //结束数据更新
            AllFiles.EndUpdate();
            stopwatch.Stop();
            long searchTime = stopwatch.ElapsedMilliseconds;

            statusLabel.Text = string.Format("    '{0}' keyWord has {1} files found spends {2} ms ", FilterString, AllFiles.Items.Count, searchTime);
            lock_Form        = false;
        }