天天看點

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);
            }
        }      

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