質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

解決済

1回答

2587閲覧

ExplorerBrowserControlで表示したディレクトリ内で検索をしたい

kntm

総合スコア58

1グッド

0クリップ

投稿2022/04/19 07:54

編集2022/04/20 01:46

Windows 10

  • .NET Framework 4.8
  • Visual Studio 2022 Community

NuGetパッケージ

  • Microsoft-WindowsAPICodePack-Core
  • Microsoft-WindowsAPICodePack-Shell
  • Microsoft-WindowsAPICodePack-ShellExtensions

WindowsFormsアプリケーションを開発しています。
基本的な動作ですが
検索用のTextBoxへ[ワード]を入力すると、その[ワード]が含まれているファイルをあらかじめ指定されているディレクトリ内を検索し一覧をコントロールに表示する
といった動作となっています。
また、コントロールに表示されているファイルを他のアプリケーションへD&Dできるようにする必要があります。
例えば、Chromeで表示されているサイト上のファイルを選択ボタンへD&Dしてアップロードできるようにといった感じです。

ListViewとImageListと使用し指定されたディレクトリに存在するファイルを検索し一覧表示することはできましたが、この実装では他のアプリケーションへのD&Dが不可能でした。

WindowsAPICodePackを使用することでForm上にエクスプローラーを表示するコントロールを追加できるということで、実装してみました。

ExplorerBrowserControlをツールボックスから追加し、実際に指定したディレクトリをエクスプローラーと同じように表示することができましたが、検索バーはどうやら表示されないようでファイルを検索して表示を絞り込むことができません。
ファイルのD&Dは実現できましたが、今度はファイルの検索ができない状況といったところです。

ファイルの検索を実現するためにはなにかプロパティの指定などが必要なのでしょうか?

WindowsAPICodePackのExplorerBrowserControlについて調べても情報があまり見つけられす、手詰まりだと感じたのでこちらで質問させていただく形となりました。

必要な機能が実装できるのであればWindowsAPICodePackの使用にこだわりはありません。
なにかいい方法があればご教示お願い致します。

ExplorerBrowserControlを使用し、指定したディレクトリを表示する部分のコードが以下となります。

C#

1private void Button_Click(object sender, EventArgs e) 2{ 3 // ディレクトリを指定 4 string location = $"{OneDrivePath}\\test\\images\\"; 5 ShellObject folder = ShellObject.FromParsingName(location); 6 7 // ナビゲーションを非表示 8 explorerBrowser1.NavigationOptions.PaneVisibility.Navigation = PaneVisibilityState.Hide; 9 10 // コマンドを非表示 11 explorerBrowser1.NavigationOptions.PaneVisibility.Commands = PaneVisibilityState.Hide; 12 13 // 指定したディレクトリを表示 14 explorerBrowser1.Navigate(folder); 15}

質問で教えて頂いたサイトを参考にListViewを使用し実装してみました。
複数選択している状態での処理がわからずfor文の中は不完全です。

C#

1 private void ListView_ItemDrag(object sender, ItemDragEventArgs e) 2 { 3 string imgdir = $"{OneDrivePath}\\test\\images\\"; 4 int items = ListView.SelectedItems.Count; 5 for (int i = 0; i < selecteditems; i++) 6 { 7 DataObject dataObj = new DataObject(DataFormats.FileDrop, $"{imgdir}{ListView.SelectedItems[i].Text}); 8 Console.WriteLine($"{imgdir}{ListView.SelectedItems[i].Text}) 9 // C:\Users\test\OneDrive\test\images\test.jpg 10 DragDropEffects effect = 11 DragDropEffects.Copy | DragDropEffects.Move; 12 ListView.DoDragDrop(dataObj, effect); 13 } 14 }

こちらの処理で単体のファイルを他アプリケーションへD&Dしてみましたが、マウスカーソルが変化はしますがそれ以外のことは何も起きませんでした。
ブラウザ上のファイル選択エリアへD&Dも結果は同じです。

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2022/04/19 09:25

> コントロールに表示されているファイルを他のアプリケーションへD&Dできるようにする必要があります。 試していないのでこちらに書きますが、これで行けませんかね? [ファイルやディレクトリをエクスプローラへドラッグ&ドロップするには? - @IT](https://atmarkit.itmedia.co.jp/fdotnet/dotnettips/384expdragdrop/expdragdrop.html)
guest

回答1

0

ベストアンサー

試していないのでこちらに書きますが、これで行けませんかね?

試しました。FileDropで行けました。
ファイルやディレクトリをエクスプローラへドラッグ&ドロップするには? - @IT


ファイルの検索を実現するためにはなにかプロパティの指定などが必要なのでしょうか?

ShellSearchFolderSearchConditionを設定すればいいようです。

WindowsAPICodePackのExplorerBrowserControlについて調べても情報があまり見つけられす

Google検索で情報がない時は、GitHubを検索するのがおすすめです。
Search · ExplorerBrowser

トップに出てきたリポがそれっぽいのと、検索機能がありそうなスクショがありました。
Total-Explorer/browserExplorerTab.cs at main · Ericvf/Total-Explorer

さらに出てきたキーワード(SearchConditionFactoryとかCreateAndOrConditionとか)をまた検索したりします。
Windows-API-Code-Pack-1.1/AdvancedSearch.xaml.cs at master · aybe/Windows-API-Code-Pack-1.1

あるいはVisual Studioでクラス等に定義ジャンプしてつらつら眺めたりします。


cs

1using System.IO; 2using System.Linq; 3using System.Text; 4using System.Windows.Forms; 5using Microsoft.WindowsAPICodePack.Controls; 6using Microsoft.WindowsAPICodePack.Controls.WindowsForms; 7using Microsoft.WindowsAPICodePack.Shell; 8using Microsoft.WindowsAPICodePack.Shell.PropertySystem; 9 10namespace Q7dnj7xj3xxesgf 11{ 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 18 File.WriteAllText("foobar.utf8.txt", "ほげぴよ", new UTF8Encoding(true)); 19 File.WriteAllText("foobar.utf8.log", "ほげぴよ", new UTF8Encoding(true)); // txtじゃないと中身まで見てくれない模様 20 File.WriteAllText("foobar.utf8n.txt", "ほげぴよ", new UTF8Encoding(false)); // bomなしも中身まで見てくれない模様 21 File.WriteAllText("foobar.sjis.txt", "ほげぴよ", Encoding.GetEncoding("shift-jis")); 22 23 var splitContainer1 = new SplitContainer { Dock = DockStyle.Fill, Parent = this, }; 24 25 var listView1 = new ListView 26 { 27 Dock = DockStyle.Fill, 28 Parent = splitContainer1.Panel1, 29 View = View.Details, 30 }; 31 listView1.Columns.Add("名前", 200); 32 foreach (var f in Directory.EnumerateFiles(Application.StartupPath)) 33 { 34 var item = new ListViewItem { Tag = f, Text = Path.GetFileName(f), }; 35 listView1.Items.Add(item); 36 } 37 listView1.ItemDrag += (s, e) => 38 { 39 var paths = listView1.SelectedItems.Cast<ListViewItem>() 40 .Select(x => (string)x.Tag).ToArray(); 41 var dataObj = new DataObject(DataFormats.FileDrop, paths); 42 listView1.DoDragDrop(dataObj, DragDropEffects.Copy); 43 }; 44 45 46 var explorerBrowser1 = new ExplorerBrowser 47 { 48 Dock = DockStyle.Fill, 49 Parent = splitContainer1.Panel2, 50 }; 51 explorerBrowser1.NavigationOptions.PaneVisibility.Navigation = PaneVisibilityState.Hide; 52 explorerBrowser1.NavigationOptions.PaneVisibility.Commands = PaneVisibilityState.Hide; 53 54 // ファイル名 Contains 55 //var sc1 = SearchConditionFactory.CreateLeafCondition(SystemProperties.System.FileName, 56 // "sjis", SearchConditionOperation.ValueContains); 57 58 // ワイルドカード 59 var sc2 = SearchConditionFactory.CreateLeafCondition(SystemProperties.System.FileName, 60 "foobar.*", SearchConditionOperation.DosWildcards); 61 62 // コンテンツ Contains 63 //var sc3 = SearchConditionFactory.CreateLeafCondition(SystemProperties.System.FullText, 64 // "ぴよ", SearchConditionOperation.ValueContains); 65 66 // 複合 67 // MDAが出るので何度か続行するか デバッグなしで実行 68 //var sc4 = SearchConditionFactory.CreateAndOrCondition(SearchConditionType.And, true, 69 // sc1, sc2); 70 71 72 var folder = (ShellContainer)ShellObject.FromParsingName(Application.StartupPath); 73 //var ssf = new ShellSearchFolder(sc1, folder); 74 var ssf = new ShellSearchFolder(sc2, folder); 75 //var ssf = new ShellSearchFolder(sc3, folder); 76 //var ssf = new ShellSearchFolder(sc4, folder); 77 78 explorerBrowser1.Navigate(ssf); 79 } 80 } 81}

追記 しょーもない解決策orz

cs

1using System.IO; 2using System.Linq; 3using System.Text; 4using System.Windows.Forms; 5using Microsoft.WindowsAPICodePack.Controls; 6using Microsoft.WindowsAPICodePack.Controls.WindowsForms; 7using Microsoft.WindowsAPICodePack.Shell; 8using Microsoft.WindowsAPICodePack.Shell.PropertySystem; 9 10namespace Q7dnj7xj3xxesgf 11{ 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 18 File.WriteAllText("foobar.utf8.txt", "ほげぴよ", new UTF8Encoding(true)); 19 File.WriteAllText("foobar.utf8.log", "ほげぴよ", new UTF8Encoding(true)); // txtじゃないと中身まで見てくれない模様 20 File.WriteAllText("foobar.utf8n.txt", "ほげぴよ", new UTF8Encoding(false)); // bomなしも中身まで見てくれない模様 21 File.WriteAllText("foobar.sjis.txt", "ほげぴよ", Encoding.GetEncoding("shift-jis")); 22 23 var splitContainer1 = new SplitContainer { Dock = DockStyle.Fill, Parent = this, }; 24 var textBox1 = new TextBox { Dock = DockStyle.Top, Parent = this, }; 25 26 var listView1 = new ListView 27 { 28 Dock = DockStyle.Fill, 29 Parent = splitContainer1.Panel1, 30 View = View.Details, 31 }; 32 listView1.Columns.Add("名前", 200); 33 foreach (var f in Directory.EnumerateFiles(Application.StartupPath)) 34 { 35 var item = new ListViewItem { Tag = f, Text = Path.GetFileName(f), }; 36 listView1.Items.Add(item); 37 } 38 listView1.ItemDrag += (s, e) => 39 { 40 var paths = listView1.SelectedItems.Cast<ListViewItem>() 41 .Select(x => (string)x.Tag).ToArray(); 42 var dataObj = new DataObject(DataFormats.FileDrop, paths); 43 listView1.DoDragDrop(dataObj, DragDropEffects.Copy); 44 }; 45 46 47 var explorerBrowser1 = new ExplorerBrowser 48 { 49 Dock = DockStyle.Fill, 50 Parent = splitContainer1.Panel2, 51 }; 52 explorerBrowser1.NavigationOptions.PaneVisibility.Navigation = PaneVisibilityState.Hide; 53 explorerBrowser1.NavigationOptions.PaneVisibility.Commands = PaneVisibilityState.Hide; 54 explorerBrowser1.NavigationComplete += (s, e) => 55 { 56 // とりあえずの飛ばした先なら本当にやりたい検索(う~~んどうなのよw 57 if (e.NewLocation == (ShellObject)KnownFolders.Computer) 58 { 59 var sc1 = SearchConditionFactory.CreateLeafCondition(SystemProperties.System.FileName, 60 textBox1.Text, SearchConditionOperation.ValueContains); 61 var folder = (ShellContainer)ShellObject.FromParsingName(Application.StartupPath); 62 var ssf = new ShellSearchFolder(sc1, folder); 63 explorerBrowser1.Navigate(ssf); 64 } 65 }; 66 67 textBox1.TextChanged += (s, e) => 68 { 69 // とりあえずどっかに飛ばす^^; 70 explorerBrowser1.Navigate((ShellObject)KnownFolders.Computer); 71 }; 72 73 explorerBrowser1.Navigate((ShellObject)KnownFolders.Computer); 74 } 75 } 76}

投稿2022/04/20 02:45

編集2023/07/30 06:53
TN8001

総合スコア9326

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kntm

2022/04/20 05:08

丁寧にどちらのパターンの処理方法をありがとうございます! WindowsAPICodePackでの実装例を使わせていただき、検索結果を無事表示させることができました。 連続でTextBoxを使用し違うワードで検索を試したところ、検索結果が変わらないという症状が起きました。 たまに正しい検索結果が表示されますが、基本的には最後に正しく検索できた結果がそのまま表示されます。 検索を行い画面の更新は行われているようなのですが検索結果が前回のまま、と言った感じです。 何度か試すとたまに正しく検索結果を表示してくれるということもあり、原因がよくわからず困っております。
TN8001

2022/04/20 11:21 編集

確かに変わんないですね。。。 NavigationOptions.AlwaysNavigate これかな?と思ったけど何も変わらず。 あれこれいじってみたけど全然理由がわかんないですorz ツリーを出しておいて**ほかの場所を選んだ後**なら変わります。 しょーもない解決策として 1. Navigateしたいところで実際に検索したいところでなくKnownFolders.Computerにでも飛ばす。 2. NavigationCompleteで if (e.NewLocation == (ShellObject)KnownFolders.Computer) だったら実際に検索したいところへNavigate みたいにむりやり「ほかの場所を選んだ後」状態にすれば動くっちゃ動きます^^; あまりにクソすぎるとは思いますが、まともな解決法を見つけられませんでした。
kntm

2022/04/21 00:48

コントロールに表示されたファイルをダブルクリックで開く動作を挟んだ後などでも正しく検索してくれますね...謎です。 これといって大規模なプログラムというわけでもないので処理が増えても最終的に望み通りの動きをすればヨシということで提案していただいた方法を試してみます。
kntm

2022/04/21 00:58

ご提案いただいた方法で望み通りの動作をしてくれました。 一度余計な動作を挟まないといけない件については謎のままですが、ひとまずは完成です。 C#をもう少し理解できた時にでもまた調査してみようと思います。 この度は様々な処理方法での解決案を提示していただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問