Need to know how to find a file via a phrase I type into the textbox this is what I have so far what should I do to fix it
public void name_txtb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
check_btn.PreformClick();
}
}
public void check_btn_Click(object sender, EventArgs e)
{
string filepath = "C:\\books\\*.txt";
if (File.Exists(filepath))
{
pup_exists pe = new pup_exists();
pe.ShowDialog();
}
else
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
CodePudding user response:
File.Exist doesn't allow you to pass a pattern to search for a set of files. It wants you to pass a single filename.
It seems that you are looking for Directory.EnumerateFiles and then, if just the presence of a txt file is enough to start the true condition you can use Any IEnumerable extension
public void check_btn_Click(object sender, EventArgs e)
{
string filepath = "C:\\books\\*.txt";
if(Directory.EnumerateFiles(filepath).Any())
{
pup_exists pe = new pup_exists();
pe.ShowDialog();
}
else
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
However, if you are just looking for the file typed in the textbox then you just need to change how you prepare the named of the file just before calling File.Exist like so:
string filepath = Path.Combine("C:\\books", name_txtb.Text ".txt");
if (File.Exists(filepath))
....
Side note: probably a typo. It is PerformClick not PreformClick
CodePudding user response:
Directory.EnumerateFiles
didn't work use File.Exists
private void check_btn_Click(object sender, EventArgs e)
{
string text = name_txtb.Text;
string filepath = Path.Combine(@"C:\\books\\", text ".txt");
if (File.Exists(filepath))
{
pup_exists pe = new pup_exists();
pe.ShowDialog();
}
else
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
this is the code that worked for me