Reputation: 39
I have a string array variable which will take filepaths based on condition but getting error as below:
if (chkIncludeSubFolders.Checked == true)
{
string[] filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.AllDirectories);
}
else
{
string[] filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.TopDirectoryOnly);
}
foreach (string item in filePaths)//Error: The name filePaths dose not exist in current context
{
//my code
}
Upvotes: 1
Views: 1633
Reputation: 629
You need to define your filePaths variable outside of your if/else statements:
string[] filePaths;
if (chkIncludeSubFolders.Checked == true)
{
filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.AllDirectories);
}
else
{
filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.TopDirectoryOnly);
}
foreach (string item in filePaths)
{
//your code
}
Upvotes: 4
Reputation: 30902
If you're comfortable with the ?:
operator, you can omit the whole if, and have terser (and more confusing) code:
string[] filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text,
chkIncludeSubFolders.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
You can define the SearchOption
separately to make the code more readable, along these lines:
string searchOption = chkIncludeSubFolders.Checked
? SearchOption.AllDirectories
: SearchOption.TopDirectoryOnly;
string[] filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, searchOption);
Otherwise, just declare the variable before the if
. Note that this is only a declaration so you are free to have it uninitialized, as long as you will (provably) initialize it afterwards.
Since you have a simple if/else, with exactly 2 branches, and you set the value of filePaths
in both of them, the compiler is smart enough to know that the variable will definitely have a value once the if
is executed.
Upvotes: 3
Reputation: 43254
You could follow the advice of the other answers and declare filePaths
outside the if/else. However, you are repeating yourself as the only thing that differs between the two calls is the options. So set them up first and then make the call:
var searchOptions = chkIncludeSubFolders.Checked
? SearchOption.AllDirectories
: SearchOption.TopDirectoryOnly;
string[] filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, searchOptions);
foreach (string item in filePaths)
{
//my code
}
Upvotes: 11
Reputation: 35310
You need to declare your variable outside the "if" block:
string[] filePaths;
if (chkIncludeSubFolders.Checked == true)
{
filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.AllDirectories);
}
else
{
filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.TopDirectoryOnly);
}
foreach (string item in filePaths)
{
}
Upvotes: 4
Reputation: 156998
Pull the variable declaration outside the method:
string[] filePaths;
if (chkIncludeSubFolders.Checked)
{
filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.AllDirectories);
}
else
{
filePaths = Directory.GetFiles(txtPath.Text, txtFormat.Text, SearchOption.TopDirectoryOnly);
}
// You can use filePaths here
Upvotes: 11