-2

I am trying to get list of all files from some directory conditionally with option SearchOption.AllDirectories. If checkbox in my form is checked, result will contains also files from subdirectory. If checkbox is not checked, a sub-folders will be omitted. So my idea was to create variable which could contain appropriate code (string ptn = ",SearchOption.AllDirectories";). But I still get same error "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll" with additional information: path contains invalid characters. Here is my part of code:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        FolderBrowserDialog folderDlg = new FolderBrowserDialog();
        folderDlg.ShowNewFolderButton = true;
        // Show the FolderBrowserDialog.
        DialogResult result = folderDlg.ShowDialog();
        txtBx1.Text = folderDlg.SelectedPath;
        Environment.SpecialFolder root = folderDlg.RootFolder;
        //declaration and initialization of string variable
        string ptn = "";
        string flTp = @folderDlg.SelectedPath;
        //show string in texblock
        textBlock.Text = flTp;
        try {
            if(subfldr.IsChecked ?? true) {
                ptn = ",SearchOption.AllDirectories";
                //show message in textblock
                txtBlck.Text = "Subfolders are checked";
            }
            string[] fileArray = Directory.GetFiles(flTp,"*.txt"+ptn );
            ...
               }
            catch (IOException ex) 
            {
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }

Program is started "normally". But after choosing directory nothing happens. Hopefully someone can point out what I am doing wrong. Thanks in advance.

Aesalus
  • 59
  • 1
  • 4
  • 2
    That is because `SearchOption` isn't a `string` type. It is actually an `Enum`. Otherwise you need to declare `ptn` as an `Enum` not `string`. To convert, take a look at: http://stackoverflow.com/questions/1061228/c-sharp-explicit-cast-string-to-enum – Greg Feb 03 '15 at 22:50

2 Answers2

0

Try something like this:

string[] fileArray = Directory.GetFiles(flTp,"*.txt", subfldr.IsChecked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

This uses the conditional operator to select which option to use. You could break it up into separate lines to understand it better:

SearchOption option = subfldr.IsChecked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
string[] fileArray = Directory.GetFiles(flTp,"*.txt", option);
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
  • It works with `subfldr.IsChecked == true ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly`. Thanks. – Aesalus Feb 04 '15 at 21:30
0

Your declaring your pattern as a type of string. In most scenarios, that would be correct. However your attempting to use the SearchOption from System.IO. This particular type isn't a string, it is actually a Enum. That is why your encountering the error your receiving.

Rather than complex logic, ternary, or inexpressive code you should declare an Enum. You can find the information here.

You could in essence call two distinct calls:

  • SearchOption.AllDirectories
  • SearchOption.TopDirectoryOnly

I suggest that, simply due to the massive amount of logic your doing just to determine what it should do. Otherwise your option would be to convert the string to an Enum or declare an Enum to begin with:

SearchOption pattern = SearchOption.AllDirectories;
SearchOption pattern = SearchOption.TopDirectoryOnly;

The code hasn been tested, and works properly call that from System.IO. If you can, you should be able to declare like so. Then your code:

// Left out first parameters to clarify:
SearchOption pattern = SearchOption.AllDirectories;
var contents = Directory.GetFiles("...", "...", pattern);

Which I believe is what your attempting to accomplish.

// Example:
var pattern = SearchOption.TopDirectoryOnly;
if(chkSubFolder.IsChecked)
     pattern = SearchOption.AllDirectories;

var files = Directory.GetFiles("Your Path", "Your Filter", pattern);
Greg
  • 11,302
  • 2
  • 48
  • 79