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;
            }
        
        }

    }
}

 

 

 

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