Reputation: 29
Why does my code won't trigger the else condition? If I can't locate my file it won't prompt, but if it locate it prompts.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = @"C:\Personal Folders\";
string[] files = Directory.GetFiles(path,
"*3.zip*", //set sa batch
SearchOption.AllDirectories);
// Display all the files.
foreach (string file in files)
{
bool exist = File.Exists(file);
if (exist == true)
{
MessageBox.Show("File Located : " + Convert.ToString(file));
}
else
{
MessageBox.Show("File Cant Locate :");
}
}
}
Upvotes: 0
Views: 227
Reputation: 627
In the code
string[] files = Directory.GetFiles(path,
"*3.zip*", //set sa batch
SearchOption.AllDirectories);
this returns the files which are present in that directory with the matching pattern, so checking for file exists does not make sense anyways and hence your else is never executing.
Upvotes: 1