码迷,mamicode.com
首页 > 其他好文 > 详细

读取文件夹底下的所有文件,异步,同步,double双精度控制小数位,进度条用法

时间:2019-07-04 09:53:30      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:code   directory   private   dict   常用   max   action   cross   load   

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

    string strPath = "";
    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog f = new FolderBrowserDialog();
       
        f.ShowDialog();//==DialogResult.OK;
        strPath = f.SelectedPath;
        //progress();
        progressBar1.Visible = true; ;
        Thread myThread = new Thread(progress);
        myThread.IsBackground = true;
        myThread.Start(); 



    }

    public delegate void mydele(Dictionary<string, long> d, int i, string st, string PerCentage);
    double percentage;
    public void progress()
    {
        Dictionary<string, long> dic = new Dictionary<string, long>();
        percentage = 0;
        GetFile(strPath,dic,"");
        if (dic!=null)
        {
            double i = 0.00d;
            progressBar1.Maximum = dic.Count;
            foreach (var item in dic)
            {
                i++;

                double dvalue = i / dic.Count;
                percentage = Math.Round((double)dvalue * 100, 2);///双精度浮点数控制
                progressBar1.Value = int.Parse(i.ToString());
                string strmsg = string.Format("{0}:文件名:{1}_大小:{2}\r\n", "**", item.Key, item.Value);

                //mydele md = new mydele(invokeShow);
                //md.Invoke(dic, int.Parse(i.ToString()), strmsg, percentage.ToString());  ///同步


                //mydele md = new mydele(invokeShow);
                //md.BeginInvoke(dic, i, strmsg,null,new object[]{});  ///异步  常用


                this.Invoke(new Action(delegate() ///异步
                {
                    textBox1.Text += strmsg;
                    label1.Text = string.Format("总文件数{0} 已经读取到{1}个文件路径 进度{2}%", dic.Count, i, percentage);

                })); 
            }
            
            if (progressBar1.Value==progressBar1.Maximum)
            {
                progressBar1.Visible = false;
            }
        }
      



    }

    private void invokeShow(Dictionary<string, long> dic, int i, string strmsg,string strPerCentage)
    {

        try
        {
            textBox1.Text += strmsg;
            label1.Text = string.Format("总文件数{0} 已经读取到第{1}个文件路径 进度{2}%", dic.Count, i, percentage);
            if (progressBar1.Value == progressBar1.Maximum)
            {
                label1.Text = "读取完成";
            }
        }
        catch (Exception)
        {
            
        }
    }


    /// <summary>  
    /// 获取路径下所有文件以及子文件夹中文件  
    /// </summary>  
    /// <param name="path">全路径根目录</param>  
    /// <param name="FileList">存放所有文件的全路径</param>  
    /// <param name="RelativePath"></param>  
    /// <returns></returns>  
    public Dictionary<string, long> GetFile(string path, Dictionary<string, long> FileList, string RelativePath)
    {
        try
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] fil = dir.GetFiles();
            DirectoryInfo[] dii = dir.GetDirectories();
            foreach (FileInfo f in fil)
            {
                //int size = Convert.ToInt32(f.Length);  
                long size = f.Length;
                FileList.Add(f.FullName, size);//添加文件路径到列表中  
            }
            //获取子文件夹内的文件列表,递归遍历  
            foreach (DirectoryInfo d in dii)
            {
                GetFile(d.FullName, FileList, RelativePath);
            }
            return FileList;
        }
        catch (Exception ex)
        {
            return null;
        }
        
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
    }  


}

读取文件夹底下的所有文件,异步,同步,double双精度控制小数位,进度条用法

标签:code   directory   private   dict   常用   max   action   cross   load   

原文地址:https://www.cnblogs.com/VictorBlog/p/11130249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!