如何读取子文件夹和文件中包含的内容

时间:2011-05-09 08:18:55

标签: c#

我可以使用BETALIB阅读器读取文件夹文件,但是通过在folderbrowserdialog中给出路径一个来阅读它 我可以读取文件夹,但在该文件夹中有许多文件夹和文件,我想阅读子文件夹。

表示: - E:\ EXE \ TTD \ ttd1 \ ttd2 \ BSEM \ f1.dat E:\ EXE \ TTD \ ttd1 \ ttd2 \ MCDEX \ f1.dat

我想通过在folderbrowserdialog中给出路径来读取文件 EX:E:\ EXE

如果我选择路径E:\ EXE,它应该读取包含在其中的所有子文件夹和文件。

任何人都可以说我应该在代码中更改

    private void btnSourceBOMCX_Click(object sender, EventArgs e)
    {
        fbdSourceBOMCX.Description = "Please select BOMCX source folder";
        fbdSourceBOMCX.RootFolder = Environment.SpecialFolder.Desktop;
        if ((fbdSourceBOMCX.ShowDialog() != DialogResult.Cancel) || (fbdSourceBOMCX.SelectedPath != ""))
        {
            sourceBOMCX = fbdSourceBOMCX.SelectedPath;
            if (File.Exists(sourceBOMCX + "\\Master"))
            {
                txtSourceBOMCX.Text = fbdSourceBOMCX.SelectedPath;
                Interaction.SaveSetting ((new Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase()).Info.Title  , "RealTime Converter", "sourceBOMCX", fbdSourceBOMCX.SelectedPath); 
            }
            else
            {

            }


        }
    }

private void bgManual_DoWork(object sender,DoWorkEventArgs e)         {

        try
        {
            int DT = int.Parse(txtSelectedDate.Text);
            BETALIB.BLReader Reader = new BETALIB.BLReader();
            BETALIB.BLReader Reader1 = new BETALIB.BLReader();
            Reader.OpenDirectory(txtSourceBOBOMCX.Text);
            Reader.ReadMaster();
            string path = txtDestinationBOBOMCX.Text + "\\XYZ-" + txtSelectedDate.Text + ".txt";
            StreamWriter Strwriter = new StreamWriter(path);

            try
            {
                while (Reader.iMaRecordsLeft > 0)
                {
                    string SecName = Reader.sMaSecName;
                    string Symbol = Reader.sMaSecSymbol;
                    Symbol = prefix + Symbol;
                    int abc = 0;
                    int xyz = lbselectedBOBOMCX.Items.Count - 1;
                    while (abc <= xyz)
                    {
                        if (SecName == (string)lbselectedBOBOMCX.Items[abc])
                        {

                            Reader.OpenSecurityByName(Reader.sMaSecName);
                            Reader.SeekToEnd();
                            Reader.Seek(-1);
                            Reader.ReadDay();
                            float O = Reader.dSeOpen;
                            float H = Reader.dSeHigh;
                            float L = Reader.dSeLow;
                            float C = Reader.dSeClose;
                            double V = Reader.dSeVolume;
                            double OI = Reader.dSeOpenInterest;
                            string T = Reader.iSeTime.ToString();
                            string D = Reader.iSeDate.ToString();

                            label9.Text = SecName + "/";
                            if (int.Parse(D) == DT)
                            {
                                string a = string.Concat(SecName, ",", Symbol, ",", D, ",", T, ",", O, ",", H, ",", L, ",", C, ",", V, ",", OI);
                                if (SecName != "" && V != 0)
                                {
                                    Strwriter.WriteLine(a);
                                }

                            }
                        }
                        abc++;
                    }
                    Reader.ReadMaster();
                }

                Reader.CloseDirectory();
                Strwriter.Close();
                Strwriter.Dispose();
            }
            catch
            {
                Reader.CloseDirectory();
                Strwriter.Close();
                Strwriter.Dispose();
            }
        }
        catch
        {
        }

请帮帮我

提前致谢

    }

1 个答案:

答案 0 :(得分:2)

您可以使用System.IO类库DirectoryInfo和FileInfo类,逻辑如下

1)在处理目录上创建两个函数,在处理文件上创建一个函数 2)在哪个目录读取函数读取验证项目是文件还是目录 3)如果项目是目录,则递归调用自身 4)如果项目是文件,则将其发送到文件处理方法进行处理

 public void fnProcessDirectory(string strPath)
    {
        if (File.Exists(strPath))
        {
            fnProcessFile(strPath);
        }
        else if (Directory.Exists(strPath))
        {
            string[] fileEntries = Directory.GetFiles(strPath);
            string[] subdirEntries = Directory.GetDirectories(strPath);

            foreach (string fileName in fileEntries)
            {
                fnProcessFile(fileName);
            }

            foreach (string dirName in subdirEntries)
            {
                fnProcessDirectory(dirName);
            }
        }
    }

    public void fnProcessFile(string strPath)
    {
        //ProcessFile
    }