开发者

How to write all files Contain in the subfolders and in the folders in to a .txt file

开发者 https://www.devze.com 2023-03-04 15:22 出处:网络
the following code is reading all files Contain in the subfolders and in the folders. ButI need to write all files Contain in the subfolders and in the folders in to .txt file.

the following code is reading all files Contain in the subfolders and in the folders. But I need to write all files Contain in the subfolders and in the folders in to .txt file. Can any one say me how do change it .

private void btnSearchNow_Click(object sender, EventArgs e)
{
    BLSecurityFinder lSecFinder = new BLSecurityFinderCla开发者_Python百科ss();
    int iCounter = 0;

    lbselected.Items.Clear();

    lSecFinder.bScanSubDirectories = chkSubfolders.Checked;

    try
    {
        lSecFinder.FindSecurity(txtSymbol.Text, txtDirectory.Text);
        while (lSecFinder.bSecLeft)
        {
            // Insert(iCounter, lSecFinder.SecName);

            lbselected.Items.Add(new SampleData() { Name = lSecFinder.SecName });
            lbselected.DisplayMember = "Name";
            lSecFinder.FindNextSecurity();

            iCounter++;
        }
    }
    catch (System.Runtime.InteropServices.COMException ComEx)
    {
        //MessageBox.Show (ComEx.Message);
    }
    finally
    {
        lSecFinder.DestroySearchDialog();
    }
}



   private void btnClose_Click(object sender, EventArgs e)
{
    this.Close();
}

thanks in addvance


var searchPattern = "*.*";
var output = @"c:\results.txt";
var files = Directory.GetFiles(folderBrowserDialog1.SelectedPath,
    searchPattern,
    chkSubfolders.Checked ? SearchOption.AllDirectories:SearchOption.TopDirectoryOnly);
File.WriteAllLines(output, files);


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)
{
    //write the file name in the txt file
}


This will get all the folder and sub-folder filesNames. you can specify the type of file you looking for or * to get every file.

public void File_To_Text(string filepath)
{  
  string [] fname;

            fname = Directory.GetFiles(filepath, "*.*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();
            File.WriteAllLines("c:\\images.txt", fname, Encoding.UTF8);
}


Here is another version which extends your code directly:

private void btnSearchNow_Click(object sender, EventArgs e)
{
    BLSecurityFinder lSecFinder = new BLSecurityFinderClass();
    int iCounter = 0;

    lbselected.Items.Clear();

    lSecFinder.bScanSubDirectories = chkSubfolders.Checked;

    using (StreamWriter writer = new StreamWriter(@"C:\results.txt", false))
    {
        try
        {
            lSecFinder.FindSecurity(txtSymbol.Text, txtDirectory.Text);
            while (lSecFinder.bSecLeft)
            {
                // Insert(iCounter, lSecFinder.SecName);

                lbselected.Items.Add(new SampleData() { Name = lSecFinder.SecName });
                lbselected.DisplayMember = "Name";

                // assuming SecName is the full filename
                writer.WriteLine(lSecFinder.SecName); 

                lSecFinder.FindNextSecurity();

                iCounter++;
            }
        }
        catch (System.Runtime.InteropServices.COMException ComEx)
        {
            //MessageBox.Show (ComEx.Message);
        }
        finally
        {
            lSecFinder.DestroySearchDialog();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消