123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using GeekDesk.Constant;
- using GeekDesk.Plugins.EveryThing.Constant;
- using GeekDesk.Util;
- using GeekDesk.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace GeekDesk.Plugins.EveryThing
- {
- public class EveryThingUtil
- {
- //检查是否是由GeekDesk启动的EveryThing
- private static bool IsByGeekDesk = true;
- //每次加载20条
- private static long pageCount = 20;
- private static UInt32 ui = 0;
- private static Process serviceProcess = null;
- private static Process exeProcess = null;
- public static void EnableEveryThing()
- {
- string pluginsPath = Constants.PLUGINS_PATH;
- bool Is64Bit = Environment.Is64BitOperatingSystem;
- string everyThingPath = pluginsPath + "/EveryThing/" + (Is64Bit ? 64 : 32) + "/EveryThing.exe";
- new Thread(() =>
- {
- try
- {
- Thread.Sleep(2000);
- //判断EveryThing服务是否已启动
- bool enabled = false;
- Process[] processList = Process.GetProcesses();
- foreach (System.Diagnostics.Process process in processList)
- {
- if (process.ProcessName.ToUpper().Equals("EVERYTHING"))
- {
- enabled = true;
- IsByGeekDesk = false;
- break;
- }
- }
- if (!enabled)
- {
- //启动服务
- serviceProcess = new Process();
- serviceProcess.StartInfo.FileName = everyThingPath;
- serviceProcess.StartInfo.UseShellExecute = true;
- serviceProcess.StartInfo.Verb = "runas";
- serviceProcess.StartInfo.Arguments = " -svc";
- serviceProcess.Start();
- }
- Thread.Sleep(2000);
- processList = Process.GetProcesses();
- //启动程序
- exeProcess = new Process();
- exeProcess.StartInfo.FileName = everyThingPath;
- exeProcess.Start();
- int waitTime = 5000;
- while (true && waitTime > 0)
- {
- Thread.Sleep(100);
- waitTime -= 100;
- exeProcess.CloseMainWindow();
- }
- } catch (Exception e)
- {
- }
-
- }).Start();
- }
- public static void DisableEveryThing()
- {
- if (IsByGeekDesk)
- {
- if (Environment.Is64BitOperatingSystem)
- {
- EveryThing64.Everything_Exit();
- } else
- {
- EveryThing32.Everything_Exit();
- }
- }
- if (exeProcess != null) exeProcess.Kill();
- if (serviceProcess != null) serviceProcess.Kill();
- }
- public static bool HasNext()
- {
- return ui < Everything_GetNumResults();
- }
- public static ObservableCollection<IconInfo> Search(string text)
- {
- ui = 0;
- //EveryThing全盘搜索
- Everything_Reset();
- EveryThingUtil.Everything_SetSearchW(text);
- EveryThingUtil.Everything_SetRequestFlags(
- EveryThingConst.EVERYTHING_REQUEST_FILE_NAME
- | EveryThingConst.EVERYTHING_REQUEST_PATH
- | EveryThingConst.EVERYTHING_REQUEST_DATE_MODIFIED
- | EveryThingConst.EVERYTHING_REQUEST_SIZE);
- EveryThingUtil.Everything_SetSort(13);
- EveryThingUtil.Everything_QueryW(true);
- return NextPage();
- }
- public static ObservableCollection<IconInfo> NextPage()
- {
- if (ui == 0)
- {
- pageCount = 40;
- } else
- {
- pageCount = 20;
- }
- string filePath;
- const int bufsize = 260;
- StringBuilder buf = new StringBuilder(bufsize);
- ObservableCollection<IconInfo> iconBakList = new ObservableCollection<IconInfo>();
- for (long count = 0; ui < Everything_GetNumResults() && count < pageCount; count++, ui++)
- {
- buf.Clear();
- EveryThingUtil.Everything_GetResultFullPathName(ui, buf, bufsize);
- filePath = buf.ToString();
- string tempPath = filePath;
- string ext = "";
- if (!ImageUtil.IsSystemItem(filePath))
- {
- ext = System.IO.Path.GetExtension(filePath).ToLower();
- }
- if (".lnk".Equals(ext))
- {
- string targetPath = FileUtil.GetTargetPathByLnk(filePath);
- if (targetPath != null)
- {
- filePath = targetPath;
- }
- }
- string name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
- if (String.IsNullOrEmpty(name))
- {
- name = tempPath.Substring(tempPath.LastIndexOf("\\"));
- }
- IconInfo iconInfo = new IconInfo
- {
- Path_NoWrite = filePath,
- LnkPath_NoWrite = tempPath,
- BitmapImage_NoWrite = null,
- StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath),
- Name_NoWrite = name,
- };
- //缓存信息 异步加载图标
- iconBakList.Add(iconInfo);
- }
- return iconBakList;
- }
- public static UInt32 Everything_SetSearchW(string lpSearchString)
- {
- if (Environment.Is64BitOperatingSystem)
- {
- return EveryThing64.Everything_SetSearchW(lpSearchString);
- } else
- {
- return EveryThing32.Everything_SetSearchW(lpSearchString);
- }
- }
- public static void Everything_SetRequestFlags(UInt32 dwRequestFlags)
- {
- if (Environment.Is64BitOperatingSystem)
- {
- EveryThing64.Everything_SetRequestFlags(dwRequestFlags);
- }
- else
- {
- EveryThing32.Everything_SetRequestFlags(dwRequestFlags);
- }
- }
- public static void Everything_SetSort(UInt32 dwSortType)
- {
- if (Environment.Is64BitOperatingSystem)
- {
- EveryThing64.Everything_SetSort(dwSortType);
- }
- else
- {
- EveryThing32.Everything_SetSort(dwSortType);
- }
- }
- public static bool Everything_QueryW(bool bWait)
- {
- if (Environment.Is64BitOperatingSystem)
- {
- return EveryThing64.Everything_QueryW(bWait);
- }
- else
- {
- return EveryThing32.Everything_QueryW(bWait);
- }
- }
- public static UInt32 Everything_GetNumResults()
- {
- if (Environment.Is64BitOperatingSystem)
- {
- return EveryThing64.Everything_GetNumResults();
- }
- else
- {
- return EveryThing32.Everything_GetNumResults();
- }
- }
- public static void Everything_GetResultFullPathName(UInt32 nIndex, StringBuilder lpString, UInt32 nMaxCount)
- {
- if (Environment.Is64BitOperatingSystem)
- {
- EveryThing64.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
- }
- else
- {
- EveryThing32.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
- }
- }
- public static void Everything_Reset()
- {
- if (Environment.Is64BitOperatingSystem)
- {
- EveryThing64.Everything_Reset();
- }
- else
- {
- EveryThing32.Everything_Reset();
- }
- }
- }
- }
|