最近做了一个查错工具,运用了winform文件操作的知识,做了几点总结,不全面,只总结了几点项目里用过的知识(关于以下内容只是个人的理解和总结,不对的地方请多指教,有补充的可以评论留言大家一起讨论学习)。

一:基础知识

1.根据文件路径得到文件夹对象:directoryinfo di = new directoryinfo(filepath);

2.根据文件路径得到路径下所有的子目录的名称(包含路径)的集合:

string[] diarray = directory.getdirectories(filepath);//得到文件夹下的文件夹的集合

string[] fiarray = directory.getfiles(filepath);//得到文件夹下文件的集合(包含文件路径)

二:关于文件操作速度慢的原因:

因为以前没怎么用到够winform io文件这一块的操作知识,所以在项目中遇到很多问题,尤其是运行速度这一块,我发现并总结了以下几点导致程序运行缓慢的原因:

  1.封装了对文件进行操作的方法时,在传参时尽量不要传文件对象,而是传路径,因为每次实例化对象时都在程序内部占了存储,我们在传对象作为参数的时候虽然方便了,但是也占了内存了。尽量减少对文件对象的实例化

  2.实例化文件对象后,在用完之后进行释放掉。file.dispose();

  3.得到图片对象时,尽量不要用image im = image.fromfile(path);因为我做图像分辨率查错时,用了这个得到对象,225张图片判断分辨率用了三十多秒才查出来,检查了很久都没找到问题,后来发现是这句代码的问题,我改成了下面这两句代码就解决了

filestream myfilestream = new filestream(path, filemode.open, fileaccess.read);

image img = image.fromstream(myfilestream, false, false);

 

三:查错工具一:图像查错之逻辑代码

1.检查文件夹是不是空文件夹

/// <summary>
        /// 检查是否是空文件夹
        /// </summary>
        /// <param name="di"></param>
        public static void checkemptyfile(string di)
        {
            string[] filearra = directory.getfiles(di);
            string[] filesarra = directory.getdirectories(di);
            if (filesarra.length == 0 && filearra.length == 0)
            {
                signfile("空文件夹", di);
            }
        }

2.检查文件名是否连续

/// <summary>
        /// 检查文件名是否连续
        /// </summary>
        /// <param name="filepath">被判断的文件路径</param>
        /// <param name="lastfile">被判断文件夹的前一个文件路径</param>
        public static void checkfilesname(string filepath, string lastfile)
        {
            string filepath1 = filepath;
            string lastfile1 = lastfile;
            //根据文件路径得到文件名
            if (filepath.substring(filepath.lastindexof('\\') + 1).contains('.'))
            {
                filepath1 = filepath.substring(0, filepath.lastindexof('.'));
            }

            if (lastfile.substring(lastfile.lastindexof('\\') + 1).contains('.'))
            {
                lastfile1 = lastfile.substring(0, lastfile.lastindexof('.'));
            }
            //分割文件名
            list<string> fname = splitfilename(filepath1);//被判断文件
            list<string> fname = splitfilename(lastfile1);//前一个文件
            //如果两个文件名都不能被分割
            if (fname.count == 0 && fname.count == 0)
            {
                string path1 = lastfile1.substring(lastfile1.lastindexof("\\") + 1);
                string path2 = filepath1.substring(filepath1.lastindexof("\\") + 1);
                int file1 = int.parse(path1);
                int file2 = int.parse(path2);
                if ((file2 - 1) != file1)
                {
                    signfile("不连续文件", filepath);
                    return;
                }
            }
            //如果两个文件名分割的正确,即分割后数组不为空
            else if (fname.count != 0 && fname.count != 0)
            {
                if (fname[0].equals(fname[0]))//判断最后一个分隔符前的字符串相同
                {
                    int file1 = int.parse(fname[1]);
                    int file2 = int.parse(fname[1]);
                    if ((file2 - 1) != file1)
                    {
                        signfile("不连续文件", filepath);//标记文件
                    }
                }
                else
                {
                    signfile("不连续文件", filepath);//标记文件
                }
            }
            else
            {
                if (fname.count == 0)
                {
                    signfile("不连续文件", filepath);//标记文件
                    return;
                }

                if (fname.count == 0)
                {
                    signfile("不连续文件", lastfile);//标记文件
                    return;
                }
            }
        }

3.按照用户指定分隔符分割文件名

public static list<string> splitfilename(string filepath)
        {
            string filename = filepath.substring(filepath.lastindexof('\\') + 1);
            list<string> fnamearr = new list<string>();
            string fgf = toolhelper.getxmlnumber("imginspect", "fgf");
            try
            {
                fnamearr.add(filename.substring(0, filename.lastindexof(fgf)));//最后一个分隔符前的字符串
                fnamearr.add(filename.substring(filename.lastindexof(fgf) + 1));//最后一个分隔符后的字符串
            }
            catch (exception ex)
            {
                //throw;
            }
            return fnamearr;

        }

4.查询图像分辨率

/// <summary>
        /// 根据分辨率查错
        /// </summary>
        /// <param name="path"></param>
        public static void checkimagedpi(string path)
        {
            try
            {
                filestream myfilestream = new filestream(path, filemode.open, fileaccess.read);
                image img = image.fromstream(myfilestream, false, false);
                //得到用户输入的分辨率
                int dpi = int.parse(ckimagemethod.strdpi);
                //获取此图形的水平分辨率(即水平dpi)(以像素/英寸为单位)
                int dpix = (int)img.horizontalresolution;
                //垂直分辨率
                int dpiy = (int)img.verticalresolution;
                if (dpix != dpi || dpiy != dpi)
                {
                    signfile("dpi不正确图片", path);
                }
                img.dispose();
                myfilestream.dispose();
               
            }
            catch (exception ex)
            {
                toolhelper.errorinfosave("图片dpi:" + path + ex.message);
            }

        }

 5.保存错误信息到错误文档,创建txt文档保存

/// <summary>
        /// 保存错误信息到文本文档
        /// </summary>
        /// <param name="strerror">错误信息</param>
        public static void errorinfosave(string strerror)
        {
            string txtname = datetime.now.tostring("yyyymmddhhmmss") + "_error.txt";
            string txtpath = system.appdomain.currentdomain.basedirectory + @"error";
            if (!directory.exists(txtpath))
            {
                directory.createdirectory(txtpath);
            }
            filestream stream = new filestream(txtpath + @"\" + txtname, filemode.create);
            streamwriter writer = new streamwriter(stream);
            writer.writeline(strerror + "\r\n");
            writer.dispose();
            writer.close();
            stream.dispose();
            stream.close();
        }