2013-01-15 65 views
0

我正在开发一个WinForm应用程序,将在指定的源目录中的文件名字符串搜索。该问题是我需要访问该文件..访问搜索结果

示例:搜索结果a .flv或.swf - 搜索完成后,结果应该可以访问。

这是我迄今为止..

private void button1_Click(object sender, EventArgs e) 
     { 

      txtOutput.Text = ""; 

      foreach (string file in Directory.GetFiles("C:\\Users\\John\\Desktop\\Sample")) 
       if (Path.GetFileName(file).Contains(txtSearch.Text)) 
        txtOutput.Text += txtOutput.Text + file + ", "; 
     } 

有了这个代码,我能够搜索文件,但它是不可访问..也搜索的输出来了与文件的路径..(类似于c:\ users \ John \ desktop \ sample \ Filename.swf)我只需要一个文件名,而不是整个路径..

我正在使用多行文本框输出,应该我用别的东西? ..如果你有更好的建议,请帮助我。

+2

你已经在使用Path.GetFileName(文件)属性。你应该将它存储在一个变量中,并用它来追加到你的文本框中。 – Aphelion

+2

你说这个文件不是“可访问的”。你是什​​么意思?你所有的代码显示的是你将文件名添加到文本框。这听起来像它正在做你已经编程它做的。 – Pete

+0

我的意思是我需要使用或运行搜索结果..我的意思是如果它是一个视频文件,当我点击结果它应该播放视频..类似的东西..我的输出到目前为止是一个路径, c:\ users \ John \ desktop \ sample \ Filename.swf我无法访问flash视频并将其作为我的输出。 –

回答

1

如果您要查找具有特定扩展名的文件,请使用搜索模式EnumerateFilesDirectory.GetFiles方法。还可以使用Path.GetFileName从文件路径获取文件名:

var path = "C:\\Users\\John\\Desktop\\Sample"; 
txtOutput.Text = String.Join(", ", Directory.GetFiles(path, "*" + txtSearch.Text) 
              .Select(f => Path.GetFileName(f)); 

txtSearch.Text假设有搜索的文件的扩展名(即.swf.flv)。因此搜索模式将是*.swf*.flv

因此,如果您搜索文本框具有文本.swf并有在您的样本目录中的两个文件SFW,那么你会得到输出file1.swf, file2.swf


如果你要搜索的文件名任意字符串:

var path = "C:\\Users\\John\\Desktop\\Sample"; 
txtOutput.Text = 
    String.Join(", ", Directory.GetFiles(path, "*" + txtSearch.Text + "*") 
           .Select(f => Path.GetFileName(f))); 

,而是多文本框,使用列表框中显示的文件:

listBox1.DataSource = Directory.GetFiles(path, "*" + txtSearch.Text + "*") 
           .Select(f => Path.GetFileName(f)) 
           .ToList(); 

UPDATE:打开文件

private void listBox1_DoubleClick(object sender, EventArgs e) 
{ 
    var fileName = listBox1.SelectedItem as string; 
    if (fileName != null) 
    { 
     var path = Path.Combine("C:\\Users\\John\\Desktop\\Sample", fileName); 
     Process.Start(path); 
    } 
} 
+0

你可以在我的代码上添加这个吗?我似乎不明白在哪里把代码..对不起,问.. –

+0

我的文本框没有输出..可能是什么问题? –

+0

@JurelJacinto你正在尝试第一个还是最后一个选项?你在搜索texbox中有什么? –

0
private void button1_Click(object sender, EventArgs e) 
    { 

     txtOutput.Text = ""; 
     List<string> fileNames = new List<string>(); 
     foreach (string file in Directory.GetFiles("C:\\Users\\John\\Desktop\\Sample")){ 
      if (Path.GetFileName(file).Contains(txtSearch.Text)){ 
       txtOutput.Text += txtOutput.Text + file + ", "; 
       fileNames.Add(file); 
      } 
     } 

    } 

所以在这里您可以使用fileNames列表中的文件。

+0

如何?同样的输出,当我添加代码.. –

1

您是wer e越来越近,这里有一些我会做的改变:

创建一个ListBox而不是多行文本框。它可以让你处理双击,即使在一个项目上。对于我的示例,ListBox名称是ListBox1。

你的button1_Click方法改成这样:

private void button1_Click(object sender, EventArgs e) 
    { 
     // You can add your seach text right to the GetFiles command, this will only return files that match. 
     // You can set the list of of items int he ListBod to the result of GetFiles instead of having to loop through as well. 
     listBox1.Items.AddRange(Directory.GetFiles(@"C:\Users\John\Desktop\Sample", "*" + txtSearch.Text + "*")); 
    } 

然后处理ListBox1_DoubleClick:

private void listBox1_DoubleClick(object sender, EventArgs e) 
    { 
     // This will run whatever file name the user double-clicked 
     System.Diagnostics.Process.Start(listBox1.SelectedItem.ToString()); 
    } 
+0

我做了你说的,但这并不显示任何ListBox1?我需要使用一些东西吗? 我的意思是“使用System.Windows.Forms;” .. –

+0

你需要添加一个ListBox到你的表单。在设计器模式下,从工具箱中拖出一个ListBox。 –

+0

我didcourse ..但仍然没有输出它.. –