0

指定されたソースディレクトリのファイル名で文字列を検索するwinformアプリケーションを開発しています。問題は、ファイルにアクセスする必要があることです。

例:検索結果は.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のようなもの) )パス全体ではなく、ファイル名のみが必要です。

出力に複数行のテキストボックスを使用していますが、他のものを使用する必要がありますか?..より良い提案があれば、私を助けてください。

4

3 に答える 3

1

特定の拡張子を持つファイルを探している場合は、EnumerateFilesまたはDirectory.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があり、サンプルディレクトリに2つの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();

更新:ファイルを開く

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);
    }
}
于 2013-01-15T15:27:46.177 に答える
1

あなたは近づいていました、ここに私がするいくつかの変更があります:

複数行のテキストボックスの代わりにリストボックスを作成します。そのようにアイテムをダブルクリックしても処理できます。私の例では、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());
    }
于 2013-01-15T15:44:49.890 に答える
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リストのファイルを使用できます。

于 2013-01-15T15:20:31.767 に答える