Esempio n. 1
0
        /// <summary>
        /// Searches the specified key word and reset the everything API afterwards
        /// </summary>
        /// <param name="keyWord">The key word.</param>
        /// <param name="token">when cancelled the current search will stop and exit (and would not reset)</param>
        /// <param name="offset">The offset.</param>
        /// <param name="maxCount">The max count.</param>
        /// <returns></returns>
        public List <SearchResult> Search(string keyWord, CancellationToken token, int maxCount, int offset = 0)
        {
            if (string.IsNullOrEmpty(keyWord))
            {
                throw new ArgumentNullException(nameof(keyWord));
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (maxCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCount));
            }

            lock (_syncObject)
            {
                if (keyWord.StartsWith("@"))
                {
                    EverythingApiDllImport.Everything_SetRegex(true);
                    keyWord = keyWord.Substring(1);
                }

                EverythingApiDllImport.Everything_SetSearchW(keyWord);
                EverythingApiDllImport.Everything_SetOffset(offset);
                EverythingApiDllImport.Everything_SetMax(maxCount);

                if (token.IsCancellationRequested)
                {
                    return(null);
                }


                if (!EverythingApiDllImport.Everything_QueryW(true))
                {
                    CheckAndThrowExceptionOnError();
                    return(null);
                }

                var results = new List <SearchResult>();
                for (int idx = 0; idx < EverythingApiDllImport.Everything_GetNumResults(); ++idx)
                {
                    if (token.IsCancellationRequested)
                    {
                        return(null);
                    }

                    EverythingApiDllImport.Everything_GetResultFullPathNameW(idx, _buffer, BufferSize);

                    var result = new SearchResult {
                        FullPath = _buffer.ToString()
                    };
                    if (EverythingApiDllImport.Everything_IsFolderResult(idx))
                    {
                        result.Type = ResultType.Folder;
                    }
                    else if (EverythingApiDllImport.Everything_IsFileResult(idx))
                    {
                        result.Type = ResultType.File;
                    }

                    results.Add(result);
                }

                Reset();

                return(results);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Searches the specified key word and reset the everything API afterwards
        /// </summary>
        /// <param name="keyWord">The key word.</param>
        /// <param name="token">when cancelled the current search will stop and exit (and would not reset)</param>
        /// <param name="offset">The offset.</param>
        /// <param name="maxCount">The max count.</param>
        /// <returns></returns>
        public List <SearchResult> Search(string keyWord, CancellationToken token, int maxCount)
        {
            if (string.IsNullOrEmpty(keyWord))
            {
                throw new ArgumentNullException(nameof(keyWord));
            }
            if (maxCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCount));
            }
            lock (_syncObject)
            {
                if (keyWord.StartsWith("@"))
                {
                    EverythingApiDllImport.Everything_SetRegex(true);
                    keyWord = keyWord.Substring(1);
                }
                else
                {
                    EverythingApiDllImport.Everything_SetRegex(false);
                }

                EverythingApiDllImport.Everything_SetRequestFlags(RequestFlag.HighlightedFileName | RequestFlag.HighlightedFullPathAndFileName);
                EverythingApiDllImport.Everything_SetOffset(0);
                EverythingApiDllImport.Everything_SetMax(maxCount);
                EverythingApiDllImport.Everything_SetSearchW(keyWord);

                if (token.IsCancellationRequested)
                {
                    return(null);
                }


                if (!EverythingApiDllImport.Everything_QueryW(true))
                {
                    CheckAndThrowExceptionOnError();
                    return(null);
                }

                var results = new List <SearchResult>();
                int count   = EverythingApiDllImport.Everything_GetNumResults();
                for (int idx = 0; idx < count; ++idx)
                {
                    if (token.IsCancellationRequested)
                    {
                        return(null);
                    }
                    // https://www.voidtools.com/forum/viewtopic.php?t=8169
                    string fileNameHighted = Marshal.PtrToStringUni(EverythingApiDllImport.Everything_GetResultHighlightedFileNameW(idx));
                    string fullPathHighted = Marshal.PtrToStringUni(EverythingApiDllImport.Everything_GetResultHighlightedFullPathAndFileNameW(idx));
                    if (fileNameHighted == null | fullPathHighted == null)
                    {
                        CheckAndThrowExceptionOnError();
                    }
                    ConvertHightlightFormat(fileNameHighted, out List <int> fileNameHightlightData, out string fileName);
                    ConvertHightlightFormat(fullPathHighted, out List <int> fullPathHightlightData, out string fullPath);

                    var result = new SearchResult
                    {
                        FileName          = fileName,
                        FileNameHightData = fileNameHightlightData,
                        FullPath          = fullPath,
                        FullPathHightData = fullPathHightlightData,
                    };
                    if (EverythingApiDllImport.Everything_IsFolderResult(idx))
                    {
                        result.Type = ResultType.Folder;
                    }
                    else if (EverythingApiDllImport.Everything_IsFileResult(idx))
                    {
                        result.Type = ResultType.File;
                    }

                    results.Add(result);
                }

                Reset();

                return(results);
            }
        }