openfile dialog with folder and file filter for automatic processing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abz89
    New Member
    • Mar 2010
    • 21

    #1

    openfile dialog with folder and file filter for automatic processing

    hi everybody, i'm a totally newbie in c#, i've create a little program just to read the .txt file

    But how if i want to make a simple text reader takes a file from the folder using the same openfile dialog??

    the components are :
    1. open button -> openfile dialog
    2. ok button -> execute
    3. textbox -> display output


    i want that the openfile dialog filter not only text file | *.txt but also can select a folder to batch the reading .txt process in current folder.

    when the ok button pressed the program will:
    1. if the selected is a single text file:

    simply 'cat' the single text file to textbox output

    if the selected is a folder:
    will search any .txt in the folder then 'cat' the contents file to the textbox output

    thx a lot! :D
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Originally posted by abz89
    hi everybody, i'm a totally newbie in c#, i've create a little program just to read the .txt file

    But how if i want to make a simple text reader takes a file from the folder using the same openfile dialog??

    the components are :
    1. open button -> openfile dialog
    2. ok button -> execute
    3. textbox -> display output


    i want that the openfile dialog filter not only text file | *.txt but also can select a folder to batch the reading .txt process in current folder.

    when the ok button pressed the program will:
    1. if the selected is a single text file:

    simply 'cat' the single text file to textbox output

    if the selected is a folder:
    will search any .txt in the folder then 'cat' the contents file to the textbox output

    thx a lot! :D
    Hi,
    So first of all take a look at:
    Create a Text Editor in 5 minutes. C# - magic.

    Than for selecting a folder you should use FolderBrowserDi alog.

    An example would be something like this:
    Code:
     private void btnSelectFolder_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
    
                fbd.ShowNewFolderButton = false;
                fbd.ShowDialog();
    
                var textualFiles = Directory.GetFiles(fbd.SelectedPath, "*.txt");
    
                if (textualFiles.Length > 0)
                {
                    txtFileContent.Text = string.Empty;
    
                    foreach (string fileTxt in textualFiles)
                    {
                        _fileName = Path.Combine(fbd.SelectedPath, fileTxt);
                        LoadFolderContent();
                    }
                }
            }
    
            private void LoadFolderContent()
            {
                try
                {
                    TextReader tr = new StreamReader(_fileName);
                    try
                    { txtFileContent.Text += tr.ReadToEnd(); }
                    catch (Exception ex)
                    { MessageBox.Show(ex.Message); }
                    finally
                    { tr.Close(); }
                }
                catch (System.IO.FileNotFoundException ex)
                { MessageBox.Show("Sorry, the file does not exist."); }
                catch (System.UnauthorizedAccessException ex)
                { MessageBox.Show("Sorry, you lack sufficient privileges."); }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
    And use foreach keyword to output all the text files at the same time.

    Comment

    • abz89
      New Member
      • Mar 2010
      • 21

      #3
      thx u :D
      i'll try

      Comment

      Working...