欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,支持doc,xls,ppt,pdf,txt等格式的文件搜索

程序员文章站 2023-03-31 10:12:53
C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,指定目录后,遍历搜索文件和子目,现在只写了支持.DOC.DOCX.XLS.XLSX.PPT.PPTX.PDF.HTML.HTM.TXT等格式的文件搜索,读取execl 内容使用的是NPOI组件,doc,html,txt 格式的使用Str ......

C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,支持doc,xls,ppt,pdf,txt等格式的文件搜索

 

c# winfrom 写的一个搜索助手,可以按照标题和内容搜索,指定目录后,遍历搜索文件和子目,现在只写了支持.doc.docx.xls.xlsx.ppt.pptx.pdf.html.htm.txt等格式的文件搜索,读取execl 内容使用的是npoi组件,doc,html,txt 格式的使用streamreader 流文件读取方式,ppt,pdf使用的spire组件。

直接上代码

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.io;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
using system.threading;
using system.security.accesscontrol;
using npoi.ss.usermodel;
using npoi.hssf.usermodel;
using npoi.xssf.usermodel;
using spire.presentation;
using spire.pdf;

namespace search
{
    public partial class formmain : form
    {
        public formmain()
        {
            initializecomponent();
        }
        //选择文件路径
        private void button1_click(object sender, eventargs e)
        {
            try
            {
                folderbrowserdialog dialog = new folderbrowserdialog();
                dialog.description = "请选择文件路径";
                if (dialog.showdialog() == dialogresult.ok)
                {
                    string foldpath = dialog.selectedpath;
                    textbox1.text = foldpath;
                    //把选择路径存储在注册表
                    application.userappdataregistry.setvalue("zmjasearchpath", foldpath);
                }
            }
            catch (exception err)
            {
                messagebox.show(err.message);
            }
        }

        private void button2_click(object sender, eventargs e)
        {
           try
            {
                string foldpath = textbox1.text;
                if (string.isnullorempty(foldpath))
                {
                    messagebox.show("请选择文件路径!");
                    return;
                }
                string searckkey = textbox2.text;
                if (string.isnullorempty(searckkey))
                {
                    messagebox.show("请输入搜索关键字!");
                    return;
                }
                string extension = combobox1.text;
                if (!string.isnullorempty(extension))
                {
                    extension = extension.toupper();
                    switch (extension)
                    {
                        case ".html":
                            extension = ".html.htm";
                            break;
                        case ".doc":
                            extension = ".doc.docx";
                            break;
                        case ".xls":
                            extension = ".xls.xlsx";
                            break;
                        case ".ppt":
                            extension = ".ppt.pptx";
                            break;
                        default:
                            break;
                    }
                }
                else {
                    extension = ".doc.docx.xls.xlsx.ppt.pptx.pdf.html.htm.txt";
                }
                datagridview1.rows.clear();
                listdirectory(@foldpath, extension, combobox2.text, searckkey);

                lbmessage.text = "";
                messagebox.show("搜索完毕");
            }
            catch (exception err)
            {
                messagebox.show(err.message);
            }
        }

         /// <summary>
         /// 列出path路径对应的文件夹中的子文件夹和文件
         /// 然后再递归列出子文件夹内的文件和文件夹
         /// </summary>
         /// <param name="path">需要搜索文件夹的路径</param>
         public void listdirectory(string path,string extension, string coding,string searckkey)
         {
             directoryinfo thefolder = new directoryinfo(@path);
             directorysecurity s = new directorysecurity(path, accesscontrolsections.access);
            //判断目录是否 可以访问  
            if (!s.areaccessrulesprotected)
            {

                foreach (fileinfo file in thefolder.getfiles())
                {
                    if (string.isnullorempty(extension) || extension.contains(file.extension.toupper()))
                    {
                        lbmessage.text = "正在搜索文件:" + path + "\\" + file.name;
                        application.doevents();

                        #region 仅检索标题
                        if (combobox3.text == "仅标题")
                        {
                            if (file.name.contains(searckkey))
                            {
                                int index = this.datagridview1.rows.add();
                                this.datagridview1.rows[index].cells[0].value = index + 1;
                                this.datagridview1.rows[index].cells[1].value = file.name;
                                this.datagridview1.rows[index].cells[2].value = path + "\\" + file.name;
                                break;
                            }
                        }
                        #endregion

                        #region 标题和内容都检索
                        else
                        {
                            #region 检索判断标题
                            //默认检索 先搜索标题是否有,如果有,则退出循环,如果没有,再检索内容
                            if (file.name.contains(searckkey))
                            {
                                int index = this.datagridview1.rows.add();
                                this.datagridview1.rows[index].cells[0].value = index + 1;
                                this.datagridview1.rows[index].cells[1].value = file.name;
                                this.datagridview1.rows[index].cells[2].value = path + "\\" + file.name;
                                break;
                            }
                            #endregion 

                            using (filestream fs = new filestream(path + "\\" + file.name, filemode.open, fileaccess.read))
                            {
                                #region 读取execl

                                if (file.extension.toupper().contains(".xls"))
                                {
                                    try
                                    {
                                        iworkbook workbook = null;//全局workbook
                                        isheet sheet;//sheet
                                        switch (file.extension)
                                        {
                                            //xls是03,用hssfworkbook打开,.xlsx是07或者10用xssfworkbook打开
                                            case ".xls": workbook = new hssfworkbook(fs); break;
                                            case ".xlsx": workbook = new xssfworkbook(fs); break;
                                            default: break;
                                        }
                                        fs.close();//关闭文件流
                                        if (workbook != null)
                                        {
                                            int count = workbook.numberofsheets;
                                            bool bo = false; //bo初始化为假
                                            for (int index = 0; index < count; index++)
                                            {
                                                if (bo)//如果bo为真
                                                    break;//退出第一层循环
                                                sheet = workbook.getsheetat(index);//读取到指定的sheet
                                                                                   //遍历读取cell
                                                for (int i = sheet.firstrownum; i <= sheet.lastrownum; i++)
                                                {
                                                    if (bo)//如果bo为真
                                                        break;//退出第二层循环
                                                    irow row = sheet.getrow(i);//得到一行
                                                    if (row != null)
                                                    {
                                                        for (int j = row.firstcellnum; j < row.lastcellnum; j++)
                                                        {
                                                            icell cell = row.getcell(j);//得到cell
                                                            if (cell != null)//如果cell为null,则赋值为空
                                                            {
                                                                if (row.getcell(j).tostring().contains(searckkey))
                                                                {
                                                                    int gridindex = this.datagridview1.rows.add();
                                                                    this.datagridview1.rows[gridindex].cells[0].value = gridindex + 1;
                                                                    this.datagridview1.rows[gridindex].cells[1].value = file.name;
                                                                    this.datagridview1.rows[gridindex].cells[2].value = path + "\\" + file.name;
                                                                    bo = true;//bo赋为真
                                                                    break;//退出第三层循环
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        //释放资源
                                        workbook = null;
                                        sheet = null;
                                    }
                                    catch (exception err)
                                    {
                                        //messagebox.show(err.message);
                                    }
                                }
                                #endregion

                                #region 读取ppt内容
                                else if (file.extension.toupper().contains(".ppt"))
                                {
                                    try
                                    {
                                        //初始化一个presentation类实例,并加载文档
                                        presentation ppt = new presentation();
                                        ppt.loadfromfile(path + "\\" + file.name);
                                        bool bo = false;
                                        foreach (islide slide in ppt.slides)
                                        {
                                            if (bo)//如果bo为真
                                                break;//退出第一层循环
                                            foreach (spire.presentation.ishape shape in slide.shapes)
                                            {
                                                if (bo)//如果bo为真
                                                    break;//退出第一层循环
                                                if (shape is iautoshape)
                                                {
                                                    foreach (textparagraph tp in (shape as iautoshape).textframe.paragraphs)
                                                    {
                                                        if (tp.text.contains(searckkey))
                                                        {
                                                            int gridindex = this.datagridview1.rows.add();
                                                            this.datagridview1.rows[gridindex].cells[0].value = gridindex + 1;
                                                            this.datagridview1.rows[gridindex].cells[1].value = file.name;
                                                            this.datagridview1.rows[gridindex].cells[2].value = path + "\\" + file.name;
                                                            bo = true;//bo赋为真
                                                            break;//退出第三层循环
                                                        }
                                                    }
                                                }

                                            }
                                        }
                                        ppt = null; //释放资源
                                    }
                                    catch (exception err)
                                    {
                                        //messagebox.show(err.message);
                                    }
                                }
                                #endregion

                                #region 读取pdf文件
                                else if (file.extension.toupper().contains(".pdf")) {
                                    try
                                    {
                                        pdfdocument pdf= new pdfdocument();
                                        pdf.loadfromfile(@path + "\\" + file.name);
                                        foreach (pdfpagebase page in pdf.pages)
                                        {
                                            string content = page.extracttext();
                                            if (content.contains(searckkey)) {
                                                int gridindex = this.datagridview1.rows.add();
                                                this.datagridview1.rows[gridindex].cells[0].value = gridindex + 1;
                                                this.datagridview1.rows[gridindex].cells[1].value = file.name;
                                                this.datagridview1.rows[gridindex].cells[2].value = path + "\\" + file.name;
                                                break;
                                            }
                                        }
                                        pdf = null;//释放资源
                                    }
                                    catch (exception err) {

                                    }
                                }
                                #endregion

                                #region 读取其他文本文件
                                else
                                {
                                    encoding codingtype = encoding.default;
                                    if (!string.isnullorempty(coding))
                                    {
                                        codingtype = encoding.getencoding(coding.toupper());
                                    }
                                    else
                                    {
                                        codingtype = common.gettype(path + "\\" + file.name);
                                    }
                                    streamreader sr = new streamreader(fs, codingtype);
                                    string str;
                                    while ((str = sr.readline()) != null)
                                    {
                                        if (str.contains(searckkey))
                                        {
                                            int index = this.datagridview1.rows.add();
                                            this.datagridview1.rows[index].cells[0].value = index + 1;
                                            this.datagridview1.rows[index].cells[1].value = file.name;
                                            this.datagridview1.rows[index].cells[2].value = path + "\\" + file.name;
                                            sr.close();
                                            fs.close();
                                            break;
                                        }
                                    }
                                }
                                #endregion

                            }
                        }

                        #endregion
                    }
                }
            }
             //遍历文件夹
             foreach (directoryinfo nextfolder in thefolder.getdirectories())
             {
                if ((nextfolder.attributes & fileattributes.hidden) != fileattributes.hidden) {
                    listdirectory(nextfolder.fullname,extension, coding,searckkey);
                }
            }
         }

        private void openfolderandselectfile(string filefullname)
        {
            system.diagnostics.processstartinfo psi = new system.diagnostics.processstartinfo("explorer.exe");
            psi.arguments = "/e,/select," + filefullname;
            system.diagnostics.process.start(psi);
        }
        private void datagridview1_cellmousedown(object sender, datagridviewcellmouseeventargs e)
        {

        }
        //删除文件,这里是一个伪删除了,直接把文件移动到deletedata文件夹中,如果需要删除手动删除
        private void button3_click(object sender, eventargs e)
        { 
            try
            {
                int count = 0;
                int totalcount = datagridview1.rowcount;
                string foldpath = textbox1.text;
                string filderpath = @foldpath + "\\deletedata";
                string filename = "";
                if (directory.exists(filderpath) == false)
                {
                    //如果不存
                    directory.createdirectory(filderpath);
                }
                for (int i = 0; i < datagridview1.rowcount; i++)
                {
                    lbmessage.text = "正在删除文件:" + datagridview1.rows[i].cells[2].value.tostring();
                    application.doevents();
                    count += 1;
                    filename = datagridview1.rows[i].cells[1].value.tostring();
                    file.move(datagridview1.rows[i].cells[2].value.tostring(), filderpath + "\\" + filename);
                }
                datagridview1.rows.clear();
                lbmessage.text = "";
                messagebox.show("删除完毕");
            }
            catch (exception err)
            {
                messagebox.show(err.message);
            }
            finally {
                lbmessage.text = "";
            }
        }

        //清空列表
        private void button4_click(object sender, eventargs e)
        {
            datagridview1.rows.clear();
        }

        private void combobox1_selectedindexchanged(object sender, eventargs e)
        {
            string extension = combobox1.text;
            if (!string.isnullorempty(extension))
            {
                extension = extension.toupper();
                switch (extension)
                {
                    case ".html":
                        combobox2.selectedindex = combobox2.items.indexof("utf-8");
                        break;
                    case ".doc":
                        combobox2.selectedindex = combobox2.items.indexof("unicode");
                        break;
                    default:
                       
                        break;
                }
            }
        }

        //默认加载
        private void formmain_load(object sender, eventargs e)
        {
            //默认是全部搜索模式
            combobox3.selectedtext = "全部";
            //从注册表把选择路径读取出来
            textbox1.text = application.userappdataregistry.getvalue("zmjasearchpath") as string;
        }

        //双击单元格打开文件所在位置
        private void datagridview1_celldoubleclick(object sender, datagridviewcelleventargs e)
        {
            try
            {
                if (e.columnindex < 0 || e.rowindex < 0)
                {
                    messagebox.show("请选择要打开的文件");
                    return;
                }
                int index = datagridview1.currentcell.rowindex;
                system.diagnostics.processstartinfo psi = new system.diagnostics.processstartinfo("explorer.exe");
                psi.arguments = "/e,/select," + datagridview1.rows[index].cells[2].value.tostring();
                system.diagnostics.process.start(psi);
            }
            catch (exception err)
            {
                messagebox.show(err.message);
            }
        }
        //右键快捷菜单打开文件所在位置
        private void 打开文件所在位置toolstripmenuitem_click(object sender, eventargs e)
        {
            try
            {
                if (datagridview1.currentrow == null)
                {
                    messagebox.show("请选择要打开的文件");
                    return;
                }
                int index = datagridview1.currentcell.rowindex;
                system.diagnostics.processstartinfo psi = new system.diagnostics.processstartinfo("explorer.exe");
                psi.arguments = "/e,/select," + datagridview1.rows[index].cells[2].value.tostring();
                system.diagnostics.process.start(psi);
            }
            catch (exception err)
            {
                messagebox.show(err.message);
            }
        }

        //双击文本框选择路径
        private void textbox1_doubleclick(object sender, eventargs e)
        {
            try
            {
                folderbrowserdialog dialog = new folderbrowserdialog();
                dialog.description = "请选择文件路径";
                if (dialog.showdialog() == dialogresult.ok)
                {
                    string foldpath = dialog.selectedpath;
                    textbox1.text = foldpath;
                    //把选择路径存储在注册表
                    application.userappdataregistry.setvalue("zmjasearchpath", foldpath);
                }
            }
            catch (exception err)
            {
                messagebox.show(err.message);
            }
        }
    }
}

参考博客园里获取文件编码格式的类

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.text;
using system.threading.tasks;

namespace search
{
    public class common
    {
        //编码问题目前为止,基本上没人解决,就连windows的ie的自动识别有时还识别错编码呢
        //如果文件有bom则判断,如果没有就用系统默认编码,缺点:没有bom的非系统编码文件会显示乱码。   
        //调用方法: common.gettype(filename)    
        public static system.text.encoding gettype(string file_name)
        {
            using (filestream fs = new filestream(file_name, filemode.open, fileaccess.read))
            {
                system.text.encoding r = gettype(fs);
                fs.close();
                return r;
            }
        }
        /// <summary> 
        /// 通过给定的文件流,判断文件的编码类型 
        /// </summary> 
        /// <param name="fs">文件流</param> 
        /// <returns>文件的编码类型</returns> 
        public static system.text.encoding gettype(filestream fs)
        {
            byte[] unicode = new byte[] { 0xff, 0xfe, 0x41 };
            byte[] unicodebig = new byte[] { 0xfe, 0xff, 0x00 };
            byte[] utf8 = new byte[] { 0xef, 0xbb, 0xbf }; //带bom 
            encoding reval = encoding.default;

            binaryreader r = new binaryreader(fs, system.text.encoding.default);
            int i;
            int.tryparse(fs.length.tostring(), out i);
            byte[] ss = r.readbytes(i);
            if (isutf8bytes(ss) || (ss[0] == 0xef && ss[1] == 0xbb && ss[2] == 0xbf))
            {
                reval = encoding.utf8;
            }
            else if (ss[0] == 0xfe && ss[1] == 0xff && ss[2] == 0x00)
            {
                reval = encoding.bigendianunicode;
            }
            else if (ss[0] == 0xff && ss[1] == 0xfe && ss[2] == 0x41)
            {
                reval = encoding.unicode;
            }
            r.close();
            return reval;

        }

        /// <summary> 
        /// 判断是否是不带 bom 的 utf8 格式 
        /// </summary> 
        /// <param name=“data“></param> 
        /// <returns></returns> 
        private static bool isutf8bytes(byte[] data)
        {
            int charbytecounter = 1; //计算当前正分析的字符应还有的字节数 
            byte curbyte; //当前分析的字节. 
            for (int i = 0; i < data.length; i++)
            {
                curbyte = data[i];
                if (charbytecounter == 1)
                {
                    if (curbyte >= 0x80)
                    {
                        //判断当前 
                        while (((curbyte <<= 1) & 0x80) != 0)
                        {
                            charbytecounter++;
                        }
                        //标记位首位若为非0 则至少以2个1开始 如:110xxxxx...........1111110x 
                        if (charbytecounter == 1 || charbytecounter > 6)
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    //若是utf-8 此时第一位必须为1 
                    if ((curbyte & 0xc0) != 0x80)
                    {
                        return false;
                    }
                    charbytecounter--;
                }
            }
            if (charbytecounter > 1)
            {
                throw new exception("非预期的byte格式");
            }
            return true;
        }
    }
}