본문 바로가기
C#

[C#] Everything SDK 사용

by Jcoder 2021. 7. 25.

SDK 다운 : SDK - voidtools

 

SDK - voidtools

 

www.voidtools.com

 

## Everything이 background에 실행 중이어야 한다. 실행 중이 아니라면 ipc 에러가 나온다.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DunamuCommon.EveryThing
{
    public class EverythingAPI
    {
        const string EVERYTHING_DLL_NAME = "Everything64.dll";
        const int EVERYTHING_OK = 0;
        const int EVERYTHING_ERROR_MEMORY = 1;
        const int EVERYTHING_ERROR_IPC = 2;
        const int EVERYTHING_ERROR_REGISTERCLASSEX = 3;
        const int EVERYTHING_ERROR_CREATEWINDOW = 4;
        const int EVERYTHING_ERROR_CREATETHREAD = 5;
        const int EVERYTHING_ERROR_INVALIDINDEX = 6;
        const int EVERYTHING_ERROR_INVALIDCALL = 7;

        const int EVERYTHING_REQUEST_FILE_NAME = 0x00000001;
        const int EVERYTHING_REQUEST_PATH = 0x00000002;
        const int EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004;
        const int EVERYTHING_REQUEST_EXTENSION = 0x00000008;
        const int EVERYTHING_REQUEST_SIZE = 0x00000010;
        const int EVERYTHING_REQUEST_DATE_CREATED = 0x00000020;
        const int EVERYTHING_REQUEST_DATE_MODIFIED = 0x00000040;
        const int EVERYTHING_REQUEST_DATE_ACCESSED = 0x00000080;
        const int EVERYTHING_REQUEST_ATTRIBUTES = 0x00000100;
        const int EVERYTHING_REQUEST_FILE_LIST_FILE_NAME = 0x00000200;
        const int EVERYTHING_REQUEST_RUN_COUNT = 0x00000400;
        const int EVERYTHING_REQUEST_DATE_RUN = 0x00000800;
        const int EVERYTHING_REQUEST_DATE_RECENTLY_CHANGED = 0x00001000;
        const int EVERYTHING_REQUEST_HIGHLIGHTED_FILE_NAME = 0x00002000;
        const int EVERYTHING_REQUEST_HIGHLIGHTED_PATH = 0x00004000;
        const int EVERYTHING_REQUEST_HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000;

        const int EVERYTHING_SORT_NAME_ASCENDING = 1;
        const int EVERYTHING_SORT_NAME_DESCENDING = 2;
        const int EVERYTHING_SORT_PATH_ASCENDING = 3;
        const int EVERYTHING_SORT_PATH_DESCENDING = 4;
        const int EVERYTHING_SORT_SIZE_ASCENDING = 5;
        const int EVERYTHING_SORT_SIZE_DESCENDING = 6;
        const int EVERYTHING_SORT_EXTENSION_ASCENDING = 7;
        const int EVERYTHING_SORT_EXTENSION_DESCENDING = 8;
        const int EVERYTHING_SORT_TYPE_NAME_ASCENDING = 9;
        const int EVERYTHING_SORT_TYPE_NAME_DESCENDING = 10;
        const int EVERYTHING_SORT_DATE_CREATED_ASCENDING = 11;
        const int EVERYTHING_SORT_DATE_CREATED_DESCENDING = 12;
        const int EVERYTHING_SORT_DATE_MODIFIED_ASCENDING = 13;
        const int EVERYTHING_SORT_DATE_MODIFIED_DESCENDING = 14;
        const int EVERYTHING_SORT_ATTRIBUTES_ASCENDING = 15;
        const int EVERYTHING_SORT_ATTRIBUTES_DESCENDING = 16;
        const int EVERYTHING_SORT_FILE_LIST_FILENAME_ASCENDING = 17;
        const int EVERYTHING_SORT_FILE_LIST_FILENAME_DESCENDING = 18;
        const int EVERYTHING_SORT_RUN_COUNT_ASCENDING = 19;
        const int EVERYTHING_SORT_RUN_COUNT_DESCENDING = 20;
        const int EVERYTHING_SORT_DATE_RECENTLY_CHANGED_ASCENDING = 21;
        const int EVERYTHING_SORT_DATE_RECENTLY_CHANGED_DESCENDING = 22;
        const int EVERYTHING_SORT_DATE_ACCESSED_ASCENDING = 23;
        const int EVERYTHING_SORT_DATE_ACCESSED_DESCENDING = 24;
        const int EVERYTHING_SORT_DATE_RUN_ASCENDING = 25;
        const int EVERYTHING_SORT_DATE_RUN_DESCENDING = 26;

        const int EVERYTHING_TARGET_MACHINE_X86 = 1;
        const int EVERYTHING_TARGET_MACHINE_X64 = 2;
        const int EVERYTHING_TARGET_MACHINE_ARM = 3;

        #region DllImport
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_SetSearch(string lpSearchString);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SetMatchPath(bool bEnable);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SetMatchCase(bool bEnable);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SetMatchWholeWord(bool bEnable);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SetRegex(bool bEnable);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SetMax(int dwMax);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SetOffset(int dwOffset);

        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_GetMatchPath();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_GetMatchCase();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_GetMatchWholeWord();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_GetRegex();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern UInt32 Everything_GetMax();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern UInt32 Everything_GetOffset();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern string Everything_GetSearch();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern StateCode Everything_GetLastError();

        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_Query();

        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_SortResultsByPath();

        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_GetNumFileResults();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_GetNumFolderResults();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_GetNumResults();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_GetTotFileResults();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_GetTotFolderResults();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern int Everything_GetTotResults();
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_IsVolumeResult(int nIndex);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_IsFolderResult(int nIndex);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern bool Everything_IsFileResult(int nIndex);
        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_GetResultFullPathName(int nIndex, StringBuilder lpString, int nMaxCount);
        //private static extern void Everything_GetResultFullPathName(int nIndex, out string lpString, int nMaxCount);
        [DllImport(EVERYTHING_DLL_NAME)]
        public static extern string Everything_GetResultFileName(int nIndex);

        [DllImport(EVERYTHING_DLL_NAME)]
        private static extern void Everything_Reset();
        // Everything 1.4
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetSort(int dwSortType);
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetSort();
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetResultListSort();
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetRequestFlags(int dwRequestFlags);
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetRequestFlags();
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetResultListRequestFlags();
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern string Everything_GetResultExtension(int nIndex);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultSize(int nIndex, out long lpFileSize);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateCreated(int nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateModified(int nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateAccessed(int nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetResultAttributes(int nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern string Everything_GetResultFileListFileName(int nIndex);
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetResultRunCount(int nIndex);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateRun(int nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateRecentlyChanged(int nIndex, out long lpFileTime);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern string Everything_GetResultHighlightedFileName(int nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern string Everything_GetResultHighlightedPath(int nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern string Everything_GetResultHighlightedFullPathAndFileName(int nIndex);
        [DllImport("Everything64.dll")]
        public static extern int Everything_GetRunCountFromFileName(string lpFileName);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_SetRunCountFromFileName(string lpFileName, int dwRunCount);
        [DllImport("Everything64.dll")]
        public static extern int Everything_IncRunCountFromFileName(string lpFileName);
        [DllImport("Everything64.dll")]
        public static extern string Everything_GetResultPath(int nIndex);
        #endregion

        #region Enum
        enum StateCode
        {
            OK,
            MemoryError,
            IPCError,
            RegisterClassExError,
            CreateWindowError,
            CreateThreadError,
            InvalidIndexError,
            InvalidCallError
        }
        #endregion

        #region Property

        /// <summary>
        /// Gets or sets a value indicating whether [match path].
        /// </summary>
        /// <value><c>true</c> if [match path]; otherwise, <c>false</c>.</value>
        public bool MatchPath
        {
            get => Everything_GetMatchPath();
            set => Everything_SetMatchPath(value);
        }

        /// <summary>
        /// Gets or sets a value indicating whether [match case].
        /// </summary>
        /// <value><c>true</c> if [match case]; otherwise, <c>false</c>.</value>
        public bool MatchCase
        {
            get => Everything_GetMatchCase();
            set => Everything_SetMatchCase(value);
        }

        /// <summary>
        /// Gets or sets a value indicating whether [match whole word].
        /// </summary>
        /// <value><c>true</c> if [match whole word]; otherwise, <c>false</c>.</value>
        public bool MatchWholeWord
        {
            get => Everything_GetMatchWholeWord();
            set => Everything_SetMatchWholeWord(value);
        }

        /// <summary>
        /// Gets or sets a value indicating whether [enable regex].
        /// </summary>
        /// <value><c>true</c> if [enable regex]; otherwise, <c>false</c>.</value>
        public bool EnableRegex
        {
            get => Everything_GetRegex();
            set => Everything_SetRegex(value);
        }
        #endregion

        public List<CustomFileInfo> Search(string keyWord)
        {
            if (string.IsNullOrEmpty(keyWord))
            {
                throw new ArgumentNullException("keyWord");
            }

            Everything_SetSearch(keyWord);
            // request name and size
            Everything_SetRequestFlags(EVERYTHING_REQUEST_FILE_NAME | EVERYTHING_REQUEST_PATH | EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_EXTENSION);

            Everything_SetSort(EVERYTHING_SORT_NAME_ASCENDING);
            Everything_SetOffset(0);
            Everything_SetMax(int.MaxValue);

            // execute the query
            if (!Everything_Query())
            {
                switch (Everything_GetLastError())
                {
                    case StateCode.CreateThreadError:
                        throw new CreateThreadException();
                    case StateCode.CreateWindowError:
                        throw new CreateWindowException();
                    case StateCode.InvalidCallError:
                        throw new InvalidCallException();
                    case StateCode.InvalidIndexError:
                        throw new InvalidIndexException();
                    case StateCode.IPCError:
                        throw new IPCErrorException();
                    case StateCode.MemoryError:
                        throw new MemoryErrorException();
                    case StateCode.RegisterClassExError:
                        throw new RegisterClassExException();
                }
                return null;
            }

            int numSize = Everything_GetNumResults();

            if (numSize <= 0)
            {
                return null;
            }

            List<CustomFileInfo> customFileInfos = new List<CustomFileInfo>(numSize);

            StringBuilder sb = new StringBuilder();
            string fullPathName;

            for (int idx = 0; idx < numSize; ++idx)
            {
                CustomFileInfo customFileInfo = new CustomFileInfo();
                Everything_GetResultFullPathName(idx, sb, 256);

                fullPathName = sb.ToString();
                if (Everything_IsFolderResult(idx))
                {
                    customFileInfo.FileSize = -1;
                    customFileInfo.FullPath = fullPathName;
                    customFileInfo.DirectoryPath = fullPathName;
                    customFileInfo.ModifiedTime = System.IO.Directory.GetLastWriteTime(fullPathName).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullPathName);

                    customFileInfo.IsFile = true;
                    customFileInfo.FileSize = fileInfo.Length;
                    customFileInfo.FileName = fileInfo.Name;
                    customFileInfo.FullPath = fileInfo.FullName;
                    customFileInfo.Extension = fileInfo.Extension;
                    customFileInfo.DirectoryPath = fileInfo.DirectoryName;
                    customFileInfo.ModifiedTime = fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                }
                customFileInfos.Add(customFileInfo);
            }
            return customFileInfos;
        }

        public class MemoryErrorException : ApplicationException
        {
        }

        /// &lt;summary&gt;
        ///  /// &lt;/summary&gt;
        public class IPCErrorException : ApplicationException
        {
        }

        /// &lt;summary&gt;
        ///  /// &lt;/summary&gt;
        public class RegisterClassExException : ApplicationException
        {
        }

        /// &lt;summary&gt;
        ///  /// &lt;/summary&gt;
        public class CreateWindowException : ApplicationException
        {
        }

        /// &lt;summary&gt;
        ///  /// &lt;/summary&gt;
        public class CreateThreadException : ApplicationException
        {
        }

        /// &lt;summary&gt;
        ///  /// &lt;/summary&gt;
        public class InvalidIndexException : ApplicationException
        {
        }

        /// &lt;summary&gt;
        ///  /// &lt;/summary&gt;
        public class InvalidCallException : ApplicationException
        {
        }
    }

    public record CustomFileInfo
    {
        public long FileSize = 0;
        public string Extension = string.Empty;
        public string FileName = string.Empty;
        public string DirectoryPath = string.Empty;
        public string FullPath = string.Empty;
        public string ModifiedTime = string.Empty;
        public bool IsFile = false;
    }
}

'C#' 카테고리의 다른 글

[C#] Webview2 STATUS_INVALID_IMAGE_HASH 오류 해결  (0) 2021.07.26
[C#] IP 변경  (0) 2021.07.25
[C#] Slack Message 보내기  (0) 2021.06.01
[C#] Mail 보내기  (0) 2021.05.04
[C#] .NET Framework 버전 확인  (0) 2021.04.27