voipservicesolution
voipservicesolution

Reputation: 27

how to read subfolders and files contain in it

i can read the folder files by using BETALIB reader, but its to big to read by giving the path one by in the folderbrowserdialog i can read folder but in that folder there are many folders and files i want to read the subfolders .

for eaxample :- E:\EXE\ttd\ttd1\ttd2\bsem\f1.dat E:\EXE\ttd\ttd1\ttd2\MCDEX\f1.dat

i want to read the files by giving the path in the folderbrowserdialog EX:E:\EXE

if i select the path E:\EXE it should read all the subfolders and files contain in it.

can any one say me what i should change in the code

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

Please help me

Thanks in advance

    }

Upvotes: 0

Views: 2625

Answers (1)

Jatin Trikha
Jatin Trikha

Reputation: 71

you can use System.IO class library DirectoryInfo and FileInfo class and the logic goes as follows

1) Create two functions on to process directory and one to process file 2) In which directory read function reads validate if the item is file or directory 3) If the item is directory it recursively calls itself 4) If the item is file it send it to file process method for processing

 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
    }

Upvotes: 2

Related Questions