none
FileInfo[] 读取多文件出现假死现象, 几个文件没事 ,多了就会假死, RRS feed

  • 问题

  •  FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                string foldPath="";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    foldPath = dialog.SelectedPath;

                   // MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                richTextBox1.Text = foldPath+"\n";

               DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
               // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                FileInfo[] fileInfo = theFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {
                    richTextBox1.Text+=(NextFile.Name)+"\n";//输出文件名
                }

                            
    2016年12月12日 7:31

答案

  • 将上面的代码封装在后台线程中,比如BackgroundWorker

    http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    • 已标记为答案 ning050089 2016年12月12日 10:36
    2016年12月12日 7:33
  • BackgroundWorker 

    这个东西 不会返回值  还要自定义 

    比如   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)//这个东西只 读取值 不会返回值
            {

                //vola = "5464646";
                // READname();

                vola = "";
                vola += foldPath + "\n";
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
                                                                      // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                FileInfo[] fileInfo = theFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {

                    vola += (NextFile.Name) + "\n";
                      richTextBox1.Text += (NextFile.Name) + "\n";
                    //    richTextBox1.SelectionStart = richTextBox1.Text.Length;
                    //    richTextBox1.ScrollToCaret();
                }
            }

    这里面的   richTextBox1.Text  不会 有数据    但是vola 就会有数据

    再 用

       private void button6_Click(object sender, EventArgs e)
            {
                richTextBox1.Text = vola;
            }

    这样richtextbox 中才会有数据  并且 这个东西 加快了读取速度

    用下面的办法 多数据就会出现光标跟随,不能 移动窗体,  读取速度慢 

    如果不加光标跟随会出现假死!!

      foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {

                   // vola += (NextFile.Name) + "\n";
                   richTextBox1.Text += (NextFile.Name) + "\n";
                      richTextBox1.SelectionStart = richTextBox1.Text.Length;// 获取数据的长度   定位  
                      richTextBox1.ScrollToCaret();// 光标实时跟踪
                }

    下面是 执行完后的正确代码 带执行输出的

     string foldPath = "";
            private void button3_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    foldPath = dialog.SelectedPath;
                 
                   // MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                richTextBox1.Text= foldPath + "\n";
                vola = foldPath+"\n";
                //DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
                //                                                      // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                //FileInfo[] fileInfo = theFolder.GetFiles();
                //foreach (FileInfo NextFile in fileInfo)  //遍历文件
                //{
                //    richTextBox1.Text += (NextFile.Name) + "\n";
                //}

            }
           
            string vola;
            
          string READname()//这里 不一样的地方 就是STRing 的 数据返回而已
            {
                string llll="";
                vola = "";
                vola += foldPath+"\n";
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
                                                                      // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                FileInfo[] fileInfo = theFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {
                   
                    vola+= (NextFile.Name) + "\n";
                    llll+= (NextFile.Name) + "\n";
                    //    richTextBox1.Text += (NextFile.Name) + "\n";
                    //    richTextBox1.SelectionStart = richTextBox1.Text.Length;
                    //    richTextBox1.ScrollToCaret();
                }

                return vola;

            }
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)//这个东西只 读取值 不会返回值
            {

                e.Result = READname();    //这里是得到的值   下面有代码输出      
                MessageBox.Show("完成");
               
              
            }

            private void button4_Click(object sender, EventArgs e)
            {
               backgroundWorker1.RunWorkerAsync();           
                button4.Enabled = false;
                button5.Enabled = true;
               
            }
           
           
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)//这里为数据输出  执行完以后会有 读取的数据 输出到你想要的地方
           {

                if (e.Error != null)//是否有错误信息
                {
                    MessageBox.Show(//弹出消息对话框
                        e.Error.Message);
                }
                else if (e.Cancelled)//异步操作是否被取消
                {
                    richTextBox1.Text = "Canceled";//返回字符串对象
                }
                else
                {
                   richTextBox1.Text = e.Result.ToString();//显示结果
                }
                button4.Enabled = true;
                button5.Enabled = false;
            
            }

            private void button5_Click(object sender, EventArgs e)
            {
                backgroundWorker1.CancelAsync();//取消挂起的后台操作 容易出错的地方 一般不要点, 有读条才好使,没有读条就是 报错,具体要改默认文件
                button4.Enabled = true;
                button5.Enabled = false;//停用取消按钮           
             
            }

            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
               progressBar1.Value = e.ProgressPercentage; MessageBox.Show("完成");
            }

            private void button6_Click(object sender, EventArgs e)
            {
                richTextBox1.Text = vola;
            }
        }




    • 已标记为答案 ning050089 2016年12月12日 10:36
    • 已编辑 ning050089 2016年12月12日 11:09
    2016年12月12日 10:34

全部回复

  • 将上面的代码封装在后台线程中,比如BackgroundWorker

    http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    • 已标记为答案 ning050089 2016年12月12日 10:36
    2016年12月12日 7:33
  • BackgroundWorker 

    这个东西 不会返回值  还要自定义 

    比如   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)//这个东西只 读取值 不会返回值
            {

                //vola = "5464646";
                // READname();

                vola = "";
                vola += foldPath + "\n";
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
                                                                      // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                FileInfo[] fileInfo = theFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {

                    vola += (NextFile.Name) + "\n";
                      richTextBox1.Text += (NextFile.Name) + "\n";
                    //    richTextBox1.SelectionStart = richTextBox1.Text.Length;
                    //    richTextBox1.ScrollToCaret();
                }
            }

    这里面的   richTextBox1.Text  不会 有数据    但是vola 就会有数据

    再 用

       private void button6_Click(object sender, EventArgs e)
            {
                richTextBox1.Text = vola;
            }

    这样richtextbox 中才会有数据  并且 这个东西 加快了读取速度

    用下面的办法 多数据就会出现光标跟随,不能 移动窗体,  读取速度慢 

    如果不加光标跟随会出现假死!!

      foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {

                   // vola += (NextFile.Name) + "\n";
                   richTextBox1.Text += (NextFile.Name) + "\n";
                      richTextBox1.SelectionStart = richTextBox1.Text.Length;// 获取数据的长度   定位  
                      richTextBox1.ScrollToCaret();// 光标实时跟踪
                }

    下面是 执行完后的正确代码 带执行输出的

     string foldPath = "";
            private void button3_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    foldPath = dialog.SelectedPath;
                 
                   // MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                richTextBox1.Text= foldPath + "\n";
                vola = foldPath+"\n";
                //DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
                //                                                      // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                //FileInfo[] fileInfo = theFolder.GetFiles();
                //foreach (FileInfo NextFile in fileInfo)  //遍历文件
                //{
                //    richTextBox1.Text += (NextFile.Name) + "\n";
                //}

            }
           
            string vola;
            
          string READname()//这里 不一样的地方 就是STRing 的 数据返回而已
            {
                string llll="";
                vola = "";
                vola += foldPath+"\n";
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);// @"C:\Users\admin\Desktop\数据导出\12.12"
                                                                      // DirectoryInfo theFolder = new DirectoryInfo(@"D:\AHtele\");
                FileInfo[] fileInfo = theFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {
                   
                    vola+= (NextFile.Name) + "\n";
                    llll+= (NextFile.Name) + "\n";
                    //    richTextBox1.Text += (NextFile.Name) + "\n";
                    //    richTextBox1.SelectionStart = richTextBox1.Text.Length;
                    //    richTextBox1.ScrollToCaret();
                }

                return vola;

            }
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)//这个东西只 读取值 不会返回值
            {

                e.Result = READname();    //这里是得到的值   下面有代码输出      
                MessageBox.Show("完成");
               
              
            }

            private void button4_Click(object sender, EventArgs e)
            {
               backgroundWorker1.RunWorkerAsync();           
                button4.Enabled = false;
                button5.Enabled = true;
               
            }
           
           
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)//这里为数据输出  执行完以后会有 读取的数据 输出到你想要的地方
           {

                if (e.Error != null)//是否有错误信息
                {
                    MessageBox.Show(//弹出消息对话框
                        e.Error.Message);
                }
                else if (e.Cancelled)//异步操作是否被取消
                {
                    richTextBox1.Text = "Canceled";//返回字符串对象
                }
                else
                {
                   richTextBox1.Text = e.Result.ToString();//显示结果
                }
                button4.Enabled = true;
                button5.Enabled = false;
            
            }

            private void button5_Click(object sender, EventArgs e)
            {
                backgroundWorker1.CancelAsync();//取消挂起的后台操作 容易出错的地方 一般不要点, 有读条才好使,没有读条就是 报错,具体要改默认文件
                button4.Enabled = true;
                button5.Enabled = false;//停用取消按钮           
             
            }

            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
               progressBar1.Value = e.ProgressPercentage; MessageBox.Show("完成");
            }

            private void button6_Click(object sender, EventArgs e)
            {
                richTextBox1.Text = vola;
            }
        }




    • 已标记为答案 ning050089 2016年12月12日 10:36
    • 已编辑 ning050089 2016年12月12日 11:09
    2016年12月12日 10:34