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.