如何通过文本框短语检查文件是否存在于文件夹中
How to check if file exists in a folder via textbox phrase
需要知道如何通过我在文本框中键入的短语查找文件这是我目前所拥有的我应该如何修复它
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();
}
}
File.Exist 不允许您传递模式来搜索一组文件。它希望您传递一个文件名。
您似乎在寻找 Directory.EnumerateFiles 然后,如果仅存在 txt 文件就足以启动 true 条件,您可以使用 Any IEnumerable扩展
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();
}
}
但是,如果您只是查找在文本框中键入的文件,那么您只需要在调用 File.Exist 之前更改准备文件名的方式,如下所示:
string filepath = Path.Combine("C:\books", name_txtb.Text + ".txt");
if (File.Exists(filepath))
....
旁注:可能是错字。它是 PerformClick 而不是 PreformClick
Directory.EnumerateFiles
没用 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();
}
}
这是对我有用的代码
需要知道如何通过我在文本框中键入的短语查找文件这是我目前所拥有的我应该如何修复它
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();
}
}
File.Exist 不允许您传递模式来搜索一组文件。它希望您传递一个文件名。
您似乎在寻找 Directory.EnumerateFiles 然后,如果仅存在 txt 文件就足以启动 true 条件,您可以使用 Any IEnumerable扩展
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();
}
}
但是,如果您只是查找在文本框中键入的文件,那么您只需要在调用 File.Exist 之前更改准备文件名的方式,如下所示:
string filepath = Path.Combine("C:\books", name_txtb.Text + ".txt");
if (File.Exists(filepath))
....
旁注:可能是错字。它是 PerformClick 而不是 PreformClick
Directory.EnumerateFiles
没用 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();
}
}
这是对我有用的代码