我需要知道如何通過我在文本框中輸入的短語找到一個檔案,這是我目前的情況,我應該怎么做才能解決它
。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:ooks*.txt"。
if (File.Exists(filepath))
{
pup_exists pe = new pup_exists()。
pe.ShowDialog()。
}
else pup_exists()
{
Form3 f3 = new Form3()。
f3.ShowDialog()。
}
uj5u.com熱心網友回復:
File.Exist不允許你傳遞一個模式來搜索一組檔案。它希望你傳遞一個單一的檔案名。
看來你要找的是Directory.EnumerateFiles,然后,如果只是存在一個txt檔案就足以啟動真實條件,你可以使用Any IEnumerable擴展
。public void check_btn_Click(object sender, EventArgs e)。
{
string filepath = "C:ooks*.txt"。
if(Directory.EnumerateFiles(filepath).Any())
{
pup_exists pe = new pup_exists()。
pe.ShowDialog();
}
else[/span
{
Form3 f3 = new Form3()。
f3.ShowDialog()。
}
然而,如果你只是尋找在文本框中輸入的檔案,那么你只需要在呼叫File.Exist之前改變你準備檔案的命名方式,比如:
string filepath = Path.Combine("C:ooks", name_txtb.Text " .txt" ) 。
if (File.Exists(filepath))
....
旁注:可能是一個打字錯誤。是PerformClick而不是PreformClick
。uj5u.com熱心網友回復:
Directory.EnumerateFiles沒有作業,使用File.Exists
private void check_btn_Click(object sender, EventArgs e)。
{
string text = name_txtb.Text;
string filepath = Path.Combine(@"C:ooks", text " .txt") 。
if (File.Exists(filepath))
{
pup_exists pe = new pup_exists()。
pe.ShowDialog()。
}
else pup_exists()
{
Form3 f3 = new Form3()。
f3.ShowDialog()。
}
這是對我有用的代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/309676.html
標籤:
