C#中ListView控件的用法

1.设置视图样式

lvFiles.View = View.Details;

2.添加列

lvFiles.Columns.Add("SN", 50, HorizontalAlignment.Center);
            lvFiles.Columns.Add("Source File", 200, HorizontalAlignment.Center);
            lvFiles.Columns.Add("Dest File", 200, HorizontalAlignment.Center);
            lvFiles.Columns.Add("Found Time", 150, HorizontalAlignment.Center);

3.添加数据

        private void SearchAllFiles(string strPath)
        {
            ListViewItem item = new ListViewItem();
            int sum = 0;
            string[] folders = Directory.GetDirectories(strPath);
            foreach (string folder in folders)
            {
                if (sum % 10 == 0) Application.DoEvents();
                sum += 1;
                DirectoryInfo folderInfo = new DirectoryInfo(folder);
                //item = lvFiles.Items.Add(folderInfo.Name);
                //item.SubItems.AddRange(new string[] { folder, "0", "目录" });
                item = lvFiles.Items.Add("Folder");
                item.SubItems.AddRange(new string[] { folder, "", "" });
                string[] files = Directory.GetFiles(folder);
                foreach (string file in files)
                {
                    if (sum % 10 == 0) Application.DoEvents();
                    sum += 1;
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.Name.Substring(fileInfo.Name.Length - 4, 4).ToUpper().Equals(tbxExt.Text.ToUpper()))
                    {
                        serialNumber = count.ToString();
                        newFileName = String.Format("{0}{1:D6}{2}", tbxPrefix.Text, count, tbxExt.Text);
                        newFilePath = String.Format("{0}\\{1}", tbxDstPath.Text, newFileName);
                        File.Copy(file, newFilePath, true);
                        //item = lvFiles.Items.Add(fileInfo.Name);
                        item = lvFiles.Items.Add(serialNumber);
                        item.SubItems.AddRange(new string[] { file, newFilePath, System.DateTime.Now.ToString("G") });
                        count++;
                        tbxStartNumber.Text = count.ToString();
                    }
                }
                SearchAllFiles(folder);
            }
        }

4.清空

lvFiles.Items.Clear();

5.总结

  程序在WinXP+VS2010下调试通过。

posted on 2012-10-24 22:11  onedime  阅读(1119)  评论(0编辑  收藏  举报