44、图片杂乱无章摆放?网上图片软件也无法满足直接浏览文件夹(子文件夹)下所有图片的业务,抓狂后,我写了一个小桌面工具用来打辅助

业务场景

       上班第一时间收到来自同事的一巨大压缩包,我的天,好不容易下载完,解压一看,全都是图片,而且(子文件夹)分类超详细,不由的对同事做事赞美一番,可是后面,我的苦恼就来了,要把图片快速浏览一遍,感觉好难的样子,于是我上网翻遍看图软件,希望能直接选择文件夹,一览其下所有图片,结果全都失望至极,抓狂中

       无奈之下,一个灵感来了,我何不自己写一个工具,把所有图片全都抓取到目标路径之下,然后浏览点击下一张按钮就一马平川了,开干,设计截图如下

界面及其简单,简直就是无脑操作,原谅我没有什么设计的能力,做不了炫酷吊炸天的界面,供大家养眼

开始按钮 事件代码

        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_start_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(this.tb_source_dir.Text))
            {
                this.tb_result.Text = "请选择源文件夹";
                return;
            }
            this.btn_start.Text = "运行中…";
            Thread t = new Thread(new ThreadStart(() =>
            {
                List<FileInfo> list = FileGet.getFile(this.tb_source_dir.Text, ".png", ".bmp", ".jpg", ".gif");
                if (list != null && list.Count > 0)
                {
                    // 开始执行图片的复制粘贴至目标路径
                    if (Directory.Exists(this.tb_target_dir.Text))
                    {
                        int index = 0;
                        // 目标路径合法,才可进行后面操作
                        foreach (FileInfo fi in list)
                        {
                            index += 1;
                            this.tb_result.Text = "正在处理 第 " + index + " 张图片";
                            fi.CopyTo(this.tb_target_dir.Text + "/" + fi.Name, true);
                            //this.tb_result.Text = "操作完成";
                            Thread.Sleep(100);
                        }
                        this.tb_result.Text = "操作完成,共处理 " + index + "张图片";
                    }
                    else
                    {
                        this.tb_result.Text = "请选择目标路径";
                    }
                }

                this.btn_start.Text = "开始";
            }));
            t.IsBackground = true;
            t.Start();
        }

工具类FileGet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PictureHelper
{
    class FileGet
    {
        private static List<FileInfo> list = new List<FileInfo>();

        public static List<FileInfo> getFile(string path, params string[] extNames)
        {
            getdir(path, extNames);
            return list;
        }

        private static void getdir(string path, params string[] extNames)
        {
            try
            {
                string[] dir = Directory.GetDirectories(path); //文件夹列表   
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] file = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件   
                    {
                        for(int i=0; i<extNames.Length; i++)
	                    {
                            String ext = extNames[i].ToString();
		                    if (ext.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                            {
                                list.Add(f);
                            }
	                    }
                        
                    }
                    foreach (string d in dir)
                    {
                        getdir(d, extNames);//递归   
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        
        }

    }
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章