菜鸟在做一个测试用例管理的小程序,因为我的每个用例文档的开头都是表格,记录用例名称和编号,所以我就在指定路径的文件夹里搜索所有word文件,查找文档用例中包含关键字的文档,然后将文档的路径加到listview中。
但是这样每次都要打开那么多word文档,速度太慢了。有木有高手给个好方法呗。下面是我的程序。                 ///// <summary>
                ///// 搜索用例
                ///// </summary>
                ///// <param name="path"></param>
                ///// <param name="testcase"></param>
                //public void searchcase(string path,string testcase)
                //{
                    
                //    if (string.IsNullOrEmpty(path))
                //    {
                //        MessageBox.Show("路径不能为空");
                //    }
                //    else if (!Directory.Exists(path))
                //    {
                //        MessageBox.Show("路径有误,请重新设置");
                //    }
                //    else
                //    {
                //        DirectoryInfo dir = new DirectoryInfo(path);
                //        FileSystemInfo[] fsi = dir.GetFileSystemInfos();
                //        foreach (FileSystemInfo i in fsi)
                //        {
                            
                //            if (i is DirectoryInfo)
                //            {
                //                searchcase(i.FullName,testcase);
                //            }
                //            else
                //            {
                                
                //                string allcase = ReadWord(i.FullName);
                //                if (string.IsNullOrEmpty(i.FullName))
                //                {
                //                    FileInfo fin = new FileInfo(i.FullName);
                //                    ListViewItem li = new ListViewItem(fin.Name.ToString());
                //                    li.SubItems.Add(fin.CreationTime.ToString());
                //                    li.SubItems.Add(fin.LastWriteTime.ToString());
                //                    li.SubItems.Add(fin.FullName.ToString());
                //                    lvResult.Items.Add(li);
                //                }
                //                else  if (IsContain(allcase, testcase))
                //                {
                //                   FileInfo fin = new FileInfo(i.FullName);
                //                   ListViewItem li = new ListViewItem(fin.Name.ToString());
                //                   li.SubItems.Add(fin.CreationTime.ToString());
                //                   li.SubItems.Add(fin.LastWriteTime.ToString());
                //                   li.SubItems.Add(fin.FullName.ToString());
                //                   lvResult.Items.Add(li);
                //                 }                //            }                
                //        }                //    }
                    
                    
                    
                //}
                ///// <summary>
                ///// 读取word表格内容
                ///// </summary>
                ///// <param name="fileName"></param>
                ///// <returns></returns>
                //public string ReadWord(string fileName)
                //{
                //    string text = "";
                //    ApplicationClass cls = null;
                //    _Document doc = null;
                //    Table table = null;
                //    object missing = Missing.Value;
                //    object path = fileName;
                //    cls = new ApplicationClass();
                    
                //    try
                //    {
                //        doc = cls.Documents.Open
                //          (ref path, ref missing, ref missing, ref missing,
                //          ref missing, ref missing, ref missing, ref missing,
                //          ref missing, ref missing, ref missing, ref missing,
                //          ref missing, ref missing, ref missing, ref missing);
                //        table = doc.Tables[1];
                //        int maxrow = table.Rows.Count;
                //        for (int i = 2; i <= maxrow; i++)
                //        {
                //            text += table.Cell(i, 0).Range.Text.ToString();
                //            text = text.Substring(0, text.Length - 2);
                //            text += " ";
                //        }
                //        return text;
                //    }
                //    catch (Exception ex)
                //    {                //        return ex.Message;
                //    }
                //    finally
                //    {
                //        if (doc != null)
                //            doc.Close(ref missing, ref missing, ref missing);
                //        cls.Quit(ref missing, ref missing, ref missing);
                //    }
                //}