WIP: search directory with function callback
This commit is contained in:
parent
46428f3f80
commit
cb9f5eb663
166
IO/SmartFileSystem.cs
Normal file
166
IO/SmartFileSystem.cs
Normal file
@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.AccessControl;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RandomFileRunner.IO
|
||||
{
|
||||
public class SmartFileSystem
|
||||
{
|
||||
|
||||
|
||||
public static List<string> GetFiles(string path, string pattern)
|
||||
{
|
||||
List<string> fileList = new List<string>();
|
||||
List<string> directoryList = new List<string>();
|
||||
|
||||
directoryList.Add(path);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (directoryList.Count <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
string directory = directoryList.First();
|
||||
directoryList.RemoveAt(0);
|
||||
|
||||
if (IsDirectoryAccessible(directory))
|
||||
{
|
||||
IEnumerable<string> searchDirList = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (string item in searchDirList)
|
||||
{
|
||||
if (!IsDirectoryAccessible(item))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
directoryList.Add(item);
|
||||
}
|
||||
|
||||
foreach (string item in Directory.EnumerateFiles(directory, pattern, SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
if (!IsFileAccessible(item))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
fileList.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fileList;
|
||||
}
|
||||
|
||||
public static void GetFiles(string path, string pattern, Func<string, ulong, int, bool> callback)
|
||||
{
|
||||
List<string> directoryList = new List<string>();
|
||||
ulong searchCount = 0;
|
||||
|
||||
directoryList.Add(path);
|
||||
searchCount++;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (directoryList.Count <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
string directory = directoryList.First();
|
||||
directoryList.RemoveAt(0);
|
||||
|
||||
callback(null, searchCount, directoryList.Count);
|
||||
|
||||
if (IsDirectoryAccessible(directory))
|
||||
{
|
||||
IEnumerable<string> searchDirList = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (string item in searchDirList)
|
||||
{
|
||||
if (!IsDirectoryAccessible(item))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
directoryList.Add(item);
|
||||
searchCount++;
|
||||
|
||||
callback(null, searchCount, directoryList.Count);
|
||||
}
|
||||
|
||||
foreach (string item in Directory.EnumerateFiles(directory, pattern, SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
if (!IsFileAccessible(item))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
callback(item, searchCount, directoryList.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsFileAccessible(string filename)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filename)) return false;
|
||||
if (!File.Exists(filename)) return false;
|
||||
|
||||
try
|
||||
{
|
||||
FileInfo fi = new FileInfo(filename);
|
||||
fi.GetAccessControl();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsDirectoryAccessible(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path)) return false;
|
||||
if (!Directory.Exists(path)) return false;
|
||||
|
||||
try
|
||||
{
|
||||
DirectoryInfo di = new DirectoryInfo(path);
|
||||
di.GetAccessControl();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
2
MainForm.Designer.cs
generated
2
MainForm.Designer.cs
generated
@ -333,6 +333,8 @@ namespace RandomFileRunner
|
||||
//
|
||||
// textBox1
|
||||
//
|
||||
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.textBox1.BackColor = System.Drawing.Color.Transparent;
|
||||
this.textBox1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.textBox1.HighlightImage = ((System.Drawing.Image)(resources.GetObject("textBox1.HighlightImage")));
|
||||
|
84
MainForm.cs
84
MainForm.cs
@ -1,8 +1,11 @@
|
||||
using RyzStudio.Windows.Forms;
|
||||
using RandomFileRunner.IO;
|
||||
using RyzStudio.Windows.Forms;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace RandomFileRunner
|
||||
@ -13,7 +16,9 @@ namespace RandomFileRunner
|
||||
|
||||
protected OptionsForm optionsForm = null;
|
||||
protected bool isBusy = false;
|
||||
protected bool requestCancel = false;
|
||||
|
||||
protected List<string> foundFiles = null;
|
||||
protected Process currentProcess = null;
|
||||
|
||||
|
||||
@ -48,13 +53,13 @@ namespace RandomFileRunner
|
||||
|
||||
ThreadControl.SetValue(pictureBox1, (isBusy ? AppResource.loading_block : null));
|
||||
ThreadControl.SetEnable(textBox1, !isBusy);
|
||||
ThreadControl.SetEnable(button2, !isBusy);
|
||||
//ThreadControl.SetEnable(button2, !isBusy);
|
||||
button2.LabelText = (isBusy? "&Cancel" : "&Search");
|
||||
ThreadControl.SetEnable(button3, !isBusy);
|
||||
ThreadControl.SetEnable(button4, !isBusy);
|
||||
ThreadControl.SetEnable(memoBox1, !isBusy);
|
||||
ThreadControl.SetEnable(button5, !isBusy);
|
||||
|
||||
ThreadControl.SetEnable(button1, !isBusy);
|
||||
//ThreadControl.SetEnable(button1, !isBusy);
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,14 +77,77 @@ namespace RandomFileRunner
|
||||
memoBox1.Text += line + Environment.NewLine;
|
||||
}
|
||||
|
||||
protected bool SearchDirecory_OnFound(string file, ulong searchCount, int searchQueue)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(file))
|
||||
{
|
||||
foundFiles.Add(file);
|
||||
}
|
||||
|
||||
//ThreadControl.SetText(label2, foundFiles.Count.ToString());
|
||||
ThreadControl.SetText(label2, foundFiles.Count.ToString("#,#", System.Globalization.CultureInfo.CurrentCulture) + Environment.NewLine + searchQueue.ToString("#,#", System.Globalization.CultureInfo.CurrentCulture));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Search
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void button2_MouseClick(object sender, MouseEventArgs e)
|
||||
private async void button2_MouseClick(object sender, MouseEventArgs e)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (this.IsBusy)
|
||||
{
|
||||
requestCancel = true;
|
||||
button2.LabelText = "&Cancelling...";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.IsBusy = true;
|
||||
requestCancel = false;
|
||||
|
||||
foundFiles = new List<string>();
|
||||
currentProcess = null;
|
||||
|
||||
string[] itemList = memoBox1.Text?.Trim().Split('\n');
|
||||
for (int i=0; i< itemList.Length; i++)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(itemList[i])) continue;
|
||||
if (requestCancel) break;
|
||||
|
||||
string item = itemList[i]?.Trim();
|
||||
|
||||
if (File.Exists(item))
|
||||
{
|
||||
if (SmartFileSystem.IsFileAccessible(item))
|
||||
{
|
||||
foundFiles.Add(item);
|
||||
ThreadControl.SetText(label2, foundFiles.Count.ToString());
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (Directory.Exists(item))
|
||||
{
|
||||
SmartFileSystem.GetFiles(item, "*.txt", SearchDirecory_OnFound);
|
||||
|
||||
ThreadControl.SetText(label2, foundFiles.Count.ToString());
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ThreadControl.SetText(label2, foundFiles.Count.ToString("#,#", System.Globalization.CultureInfo.CurrentCulture) + " File" + ((foundFiles.Count == 1) ? "" : "s") + " Found");
|
||||
|
||||
this.IsBusy = false;
|
||||
requestCancel = false;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -169,10 +237,16 @@ namespace RandomFileRunner
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void button1_MouseClick(object sender, MouseEventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,26 @@
|
||||
{
|
||||
public class AppSession
|
||||
{
|
||||
public class HotKeyOptions
|
||||
{
|
||||
public bool IsCtrl { get; set; } = false;
|
||||
public bool IsAlt { get; set; } = false;
|
||||
public bool IsShift { get; set; } = false;
|
||||
public int Key { get; set; } = (int)System.Windows.Forms.Keys.None;
|
||||
|
||||
public int ModifierCode => ((this.IsAlt ? 1 : 0) + (this.IsCtrl ? 2 : 0) + (this.IsShift ? 4 : 0));
|
||||
|
||||
public System.Windows.Forms.Keys KeyCode => (System.Windows.Forms.Keys)this.Key;
|
||||
}
|
||||
|
||||
|
||||
public bool SearchTopDirectoryOnly { get; set; } = false;
|
||||
|
||||
public bool ClosePrevOnNext { get; set; } = false;
|
||||
|
||||
public HotKeyOptions NextHotKey { get; set; } = null;
|
||||
|
||||
|
||||
public bool AlwaysOnTop { get; set; } = false;
|
||||
|
||||
public int NoFrames { get; set; } = 3;
|
||||
|
Loading…
Reference in New Issue
Block a user