c# everything掃描共享盤示例


背景

由於我們公司不同部門文件都放在共享盤中,我們需要將公盤文件上傳阿里雲或ftp,做增量查詢(上傳過的數據不會在上傳)
由於掃描公盤需求越來越多,數據量越來越大,之前通過遞歸掃描磁盤的方式太慢。

所以研究通過everything搜索文件方式替換之前的方式。 研究下來發現該方式掃描磁盤速度非常快。

那么廢話不多說,直接上代碼。

 

如果需要掃描共享盤的話:

(如果是本地磁盤可以忽略1)

1.需要安裝一下everything,在選項-文件夾-添加一下你的文件目錄

2.需要將Everything32.dll copy到你的bin目錄下

3.可以copy代碼,運行

 [DllImport("Everything32.dll", CharSet = CharSet.Unicode)]
        public static extern int Everything_SetSearchW(string lpSearchString);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetMatchPath(bool bEnable);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetMatchCase(bool bEnable);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetMatchWholeWord(bool bEnable);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetRegex(bool bEnable);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetMax(int dwMax);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetOffset(int dwOffset);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetReplyWindow(IntPtr hWnd);
        [DllImport("Everything32.dll")]
        public static extern void Everything_SetReplyID(int nId);

        [DllImport("Everything32.dll")]
        public static extern bool Everything_GetMatchPath();
        [DllImport("Everything32.dll")]
        public static extern bool Everything_GetMatchCase();
        [DllImport("Everything32.dll")]
        public static extern bool Everything_GetMatchWholeWord();
        [DllImport("Everything32.dll")]
        public static extern bool Everything_GetRegex();
        [DllImport("Everything32.dll")]
        public static extern UInt32 Everything_GetMax();
        [DllImport("Everything32.dll")]
        public static extern UInt32 Everything_GetOffset();
        [DllImport("Everything32.dll")]
        public static extern string Everything_GetSearch();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetLastError();
        [DllImport("Everything32.dll")]
        public static extern IntPtr Everything_GetReplyWindow();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetReplyID();

        [DllImport("Everything32.dll")]
        public static extern bool Everything_QueryW(bool bWait);

        [DllImport("Everything32.dll")]
        public static extern bool Everything_IsQueryReply(int message, IntPtr wParam, IntPtr lParam, uint nId);

        [DllImport("Everything32.dll")]
        public static extern void Everything_SortResultsByPath();

        [DllImport("Everything32.dll")]
        public static extern int Everything_GetNumFileResults();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetNumFolderResults();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetNumResults();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetTotFileResults();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetTotFolderResults();
        [DllImport("Everything32.dll")]
        public static extern int Everything_GetTotResults();
        [DllImport("Everything32.dll")]
        public static extern bool Everything_IsVolumeResult(int nIndex);
        [DllImport("Everything32.dll")]
        public static extern bool Everything_IsFolderResult(int nIndex);
        [DllImport("Everything32.dll")]
        public static extern bool Everything_IsFileResult(int nIndex);
        [DllImport("Everything32.dll", CharSet = CharSet.Unicode)]
        public static extern void Everything_GetResultFullPathNameW(int nIndex, StringBuilder lpString, int nMaxCount);
        [DllImport("Everything32.dll")]
        public static extern void Everything_Reset();


        #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 Boolean MatchPath
        {
            get
            {
                return 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 Boolean MatchCase
        {
            get
            {
                return 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 Boolean MatchWholeWord
        {
            get
            {
                return 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 Boolean EnableRegex
        {
            get
            {
                return Everything_GetRegex();
            }
            set
            {
                Everything_SetRegex(value);
            }
        }
        #endregion


        #region Public Method
        /// <summary>
        /// Resets this instance.
        /// </summary>
        public void Reset()
        {
            Everything_Reset();
        }

        /// <summary>
        /// Searches the specified key word.
        /// </summary>
        /// <param name="keyWord">The key word.</param>
        /// <returns></returns>
        public static IEnumerable<string> Search(string keyWord)
        {
            return Search(keyWord, 0, int.MaxValue);
        }

        /// <summary>
        /// Searches the specified key word.
        /// </summary>
        /// <param name="keyWord">The key word.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="maxCount">The max count.</param>
        /// <returns></returns>
        public static IEnumerable<string> Search(string keyWord, int offset, int maxCount)
        {
            if (string.IsNullOrEmpty(keyWord))
                throw new ArgumentNullException("keyWord");

            if (offset < 0)
                throw new ArgumentOutOfRangeException("offset");

            if (maxCount < 0)
                throw new ArgumentOutOfRangeException("maxCount");

            Everything_SetSearchW(keyWord);
            Everything_SetOffset(offset);
            Everything_SetMax(maxCount);
            if (!Everything_QueryW(true))
            {
                switch (Everything_GetLastError())
                {
                    case (int)StateCode.CreateThreadError:
                        throw new CreateThreadException();
                    case (int)StateCode.CreateWindowError:
                        throw new CreateWindowException();
                    case (int)StateCode.InvalidCallError:
                        throw new InvalidCallException();
                    case (int)StateCode.InvalidIndexError:
                        throw new InvalidIndexException();
                    case (int)StateCode.IPCError:
                        throw new IPCErrorException();
                    case (int)StateCode.MemoryError:
                        throw new MemoryErrorException();
                    case (int)StateCode.RegisterClassExError:
                        throw new RegisterClassExException();
                }
                yield break;
            }

            const int bufferSize = 256;
            StringBuilder buffer = new StringBuilder(bufferSize);
            for (int idx = 0; idx < Everything_GetNumResults(); ++idx)
            {
                Everything_GetResultFullPathNameW(idx, buffer, bufferSize);
                yield return buffer.ToString();
            }
        }
        #endregion


        /// <summary>
        ///  /// </summary>
        public class MemoryErrorException : ApplicationException
        {
        }

        /// <summary>
        ///  /// </summary>
        public class IPCErrorException : ApplicationException
        {
        }

        /// <summary>
        ///  /// </summary>
        public class RegisterClassExException : ApplicationException
        {
        }

        /// <summary>
        ///  /// </summary>
        public class CreateWindowException : ApplicationException
        {
        }

        /// <summary>
        ///  /// </summary>
        public class CreateThreadException : ApplicationException
        {
        }

        /// <summary>
        ///  /// </summary>
        public class InvalidIndexException : ApplicationException
        {
        }

        /// <summary>
        ///  /// </summary>
        public class InvalidCallException : ApplicationException
        {
        }


        static void Main(string[] args)
        {
            //@"\\10.10.1.10\文件夾名稱 <*.png|jpg|bmp>"  或"c:<*.png|jpg|bmp>  dm:2019/5-2019/12為一個時間段內的文件
            var list = Search(@"文件名或目錄");
            ArrayList array = new ArrayList();
            foreach (var item in list)
            {
                array.Add(item);
            }
        }

返回的結果就是你要上傳的文件列表,如果有需要可以根據自己的需求在次拓展。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM