using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace Price
{
    public partial class Form2 : Form
    {
        Form1 f1 = null;
        int PwErrCount = 0;
        public Form2(ref Form1 f1)
        {
            this.f1 = f1;
            InitializeComponent();
        }        private void Form2_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            this.label2.BackColor = Color.FromArgb(0, Color.Transparent);
            Text = f1.WindowTitle.Trim();
            Thread t = new Thread(FileRead);
            t.IsBackground = true;
            t.Start();        }
        private void FileRead()
        {
            DirectoryInfo theFolder = new DirectoryInfo(f1.Path.Trim());
            var dirs = theFolder.GetDirectories();
            if (dirs.Length < 1)
                return;
            int FileCount = 0;
            for (int i = 0; i < dirs.Length; i++)
            {
                foreach (var item in dirs[i].GetFiles())
                {
                    if (item.Name.Contains("_a.txt"))
                        ++FileCount;
                }
            }            int FileIndex = 0;
            for (int i = 0; i < dirs.Length; i++)
            {
                DirectoryInfo di = new DirectoryInfo(dirs[i].FullName);
                var dir = new Form1.Dir();
                dir.Name = dirs[i].Name;
                dir.Time = di.CreationTime;
                foreach (var item in dirs[i].GetFiles())
                {
                    if (item.Name.Contains("_a.txt"))
                    {
                        var fs = item.OpenRead();
                        dir.Files.Add(new Form1.FileItem() { Name = item.Name, Count = f1.GetFileCount(ref fs) });
                        fs.Close();                        ++FileIndex;
                        progressBar1.Value = (int)(((double)FileIndex / (double)FileCount) * 100d);
                        label2.Text = "加载中(" + progressBar1.Value + "%)";
                        //Thread.Sleep(100);
                    }
                }
                f1.Dirs.Add(dir);
            }
            label2.Text = "加载完成";
            progressBar1.Value = 100;
            if (button1.Enabled == false && button1.Text == "等待加载完成" && textBox1.Text == f1.Password.Trim())
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != f1.Password.Trim())
            {
                ++PwErrCount;
                if (PwErrCount > 3)
                {
                    DelSelf();
                    return;
                }
                MessageBox.Show("密码输入错误");
            }
            else
            {
                if (progressBar1.Value == 100)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    button1.Enabled = false;
                    button1.Text = "等待加载完成";
                }
                
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct ShellExecuteInfo
        {
            public int size;
            public uint mask;
            public IntPtr hwnd;
            public string verb;
            public string file;
            public string parameters;
            public string directory;
            public int show;
            public IntPtr hInsApp;
            public IntPtr IDList;
            public string lpClass;
            public IntPtr hkeyClass;
            public uint dwHotKey;
            public IntPtr hIcon;
            public IntPtr hProcess;
        };
        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool ShellExecuteEx(ShellExecuteInfo info);
        private void DelSelf()
        {
            try
            {
                string CmdPath = Environment.GetEnvironmentVariable("COMSPEC");
                if (string.IsNullOrEmpty(CmdPath))
                    return;                string Arg = "/c del \"";
                Arg += System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                Arg += "\" > nul";                System.Diagnostics.Process p = new Process();                p.StartInfo.FileName = CmdPath;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.Arguments = Arg;                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
                Thread.CurrentThread.Priority = ThreadPriority.Highest;
                p.Start();
                p.PriorityClass = ProcessPriorityClass.Idle;
                Environment.Exit(0);
            }
            catch
            { 
            }
        }
    }
}读取固定文件夹下的txt文本,几万个文本现在的读取速度已经需要几分钟,如果增加跟多会更慢了。还能在提升速度吗,请大神不吝赐教