如何按名称搜索文件并打开它? [英] How search a file by its name and open it?
本文介绍了如何按名称搜索文件并打开它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
目前我在ASP.net C#做一个Web应用程序项目。
这里我有一个问题是按名称搜索文件。下面的代码显示我做了,但问题是,它没有显示根据搜索名称的文件,因为它显示目录中的所有文件名。
另一个问题是,我不知道如何打开搜索文件。任何人都可以帮助我吗?
Hi friends,
Currently i'm doing a web application project in ASP.net C#.
Here i have a problem to search a file by its name. Below code is shows were i did, but the problem is, it does not shows the file according to the search name, since it show all file name in directories.
Another problem is, i don't how to open the search files. Can any one help me?
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string[] pdffiles = Directory.GetFiles(@"\\192.168.5.10\\fbar\\REPORT\\CLOTHO\\H2\\REPORT\\", "*.pdf", SearchOption.AllDirectories);
string search = TextBox1.Text;
ListBox1.Items.Clear();
foreach (string file in pdffiles)
{
ListBox1.Items.Add(Path.GetFileName(file));
}
TextBox1.Focus();
}
else
{
Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
}
}
推荐答案
string[] pdffiles = Directory.GetFiles(@"\\192.168.5.10\\fbar\\REPORT\\CLOTHO\\H2\\REPORT\\", "*" + TextBox1.Text + "*.pdf",SearchOption.AllDirectories);
此代码将使用默认阅读器打开PDF文件。
将下面的代码放入列表框的一个事件中,例如DoubleClick。
This code will open the PDF file using the default reader.
Put the code below into one of the events of the list box, for example, DoubleClick.
ProcessStartInfo infoOpenPdf = new ProcessStartInfo();
infoOpenPdf.Verb = "open";
infoOpenPdf.FileName = pathPdf;
infoOpenPdf.CreateNoWindow = true;
infoOpenPdf.WindowStyle = ProcessWindowStyle.Normal;
Process openPdf = new Process();
openPdf.StartInfo = infoOpenPdf;
openPdf.Start();
将以下内容添加到源文件的顶部:
Add the following to the top of the source file:
using System.Diagnostics;
这篇关于如何按名称搜索文件并打开它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文