Repository navigation
Expand file tree
/
Copy pathfrmSearch.cs
More file actions
173 lines (145 loc) · 5.38 KB
/
Copy pathfrmSearch.cs
File metadata and controls
173 lines (145 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using FileSystemImage.FileTree;
using FileSystemImage.DataModels;
using Serilog;
namespace FileSystemImage
{
public partial class FrmSearch : Form
{
private FileSystemDrive _fileSystemDrive;
private CancellationTokenSource _searchCancellation = null;
private delegate void SearchCompleteEventHandler(object sender, SearchEventArgs e);
private bool _isSearching = false;
public FrmSearch()
{
InitializeComponent();
}
public void SetFileSystemDrive(FileSystemDrive fsd)
{
_fileSystemDrive = fsd;
}
private void frmSearch_Load(object sender, EventArgs e)
{
_searchCancellation = new CancellationTokenSource();
}
private void frmSearch_Resize(object sender, EventArgs e)
{
const int padding = 20;
const int innerPadding = 6;
grpSearch.Width = Width - padding * 2;
lstResults.Width = grpSearch.Width;
lstResults.Height = Height - lstResults.Top - statusStrip1.Height - padding * 2;
txtSearch.Width = txtSearch.Parent.Width - innerPadding * 2;
btnSearch.Left = btnSearch.Parent.Width - btnSearch.Width - innerPadding;
chkRegexp.Left = chkRegexp.Parent.Width - chkRegexp.Width - btnSearch.Width - innerPadding * 2;
chkIgnoreCase.Left = chkRegexp.Left;
}
private void btnSearch_Click(object sender, EventArgs e)
{
PerformSearch();
}
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
PerformSearch();
}
private void PerformSearch()
{
if (txtSearch.Text == "")
return;
if (_searchCancellation.IsCancellationRequested)
return;
if (_isSearching)
{
_searchCancellation.Cancel();
_searchCancellation.Token.Register(() =>
{
_isSearching = false;
//Invoke(new SearchCompleteEventHandler(SearchComplete), this, new SearchEventArgs(null));
});
return;
}
btnSearch.Text = "Cancel";
_isSearching = true;
var searchAlg = new TreeSearch(_fileSystemDrive);
try
{
string searchString = txtSearch.Text;
while (!chkRegexp.Checked && searchString.Contains(@"\\"))
{
searchString = searchString.Replace(@"\\", @"\");
}
bool includeFolderNames = chkFoldersIncluded.Checked;
Task.Run(async () =>
{
var searchRes = await searchAlg.Search(searchString, chkRegexp.Checked, chkIgnoreCase.Checked, includeFolderNames);
_isSearching = false;
Invoke(new SearchCompleteEventHandler(SearchComplete), this, new SearchEventArgs(searchRes));
}, _searchCancellation.Token);
}
catch (Exception ex)
{
Log.Error(ex, "SearchException");
MessageBox.Show(ex.Message);
}
}
private void SearchComplete(object sender, SearchEventArgs e)
{
btnSearch.Text = "Search";
if (e.TreeSearchResults != null)
PopulateSearchResult(e.TreeSearchResults);
}
private void PopulateSearchResult(List<TreeSearchResult> searchResults)
{
toolStripSearchResCount.Text = searchResults.Count.ToString();
bool truncate = false;
if (searchResults.Count > 5000)
{
truncate = true;
searchResults = searchResults.Take(5000).ToList();
}
lstResults.Items.Clear();
foreach (TreeSearchResult res in searchResults)
lstResults.Items.Add(res);
if (truncate)
MessageBox.Show("The Search result has been truncated to 5000 results");
}
private void lstResults_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)13) return;
var item = lstResults.SelectedItem as TreeSearchResult;
if (item != null)
{
try
{
string p = item.Path + "\\" + item.File.Name;
string args = $"/e, /select, \"{p}\"";
var info = new ProcessStartInfo
{
FileName = "explorer",
Arguments = args
};
Process.Start(info);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
internal class SearchEventArgs : EventArgs
{
public SearchEventArgs(List<TreeSearchResult> searchRes)
{
TreeSearchResults = searchRes;
}
public List<TreeSearchResult> TreeSearchResults { get; }
}
}