/// <summary> /// 执行文件扫描的线程 /// </summary> private void ListFileThread() { if (_ret == null) { _ret = new AutoResetEvent(true); } _ret.WaitOne(); using (_performCounter = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total")) { RemoveNotExistFile(); foreach (KeyValuePair <string, string> item in _sharePath) { CheckIdle(); DirectoryInfo dirInfo = new DirectoryInfo(item.Value); List <CustFileInfo> fileList = new List <CustFileInfo>(); try { if (!_shareFileDict.ContainsKey(item)) { _shareFileDict.Add(item, null); } _curPath = item; ShareFileEvent arg = new ShareFileEvent() { Path = item.Value }; if (HashingPath != null) { HashingPath(this, arg); } if (GetAll(dirInfo, ref fileList)) { if (_shareFileDict.ContainsKey(item)) { if (_shareFileDict[item] == null) { _shareFileDict[item] = new List <CustFileInfo>(); } _shareFileDict[item].AddRange(fileList); } else { _shareFileDict.Add(item, fileList); } } if (OnePathComplete != null) { OnePathComplete(this, arg); } } catch (Exception e) { Console.WriteLine(e.Message); } } if (AllScanComplete != null) { AllScanComplete(this, null); } // t.Dispose(); _performCounter.Close(); } _ret.Set(); }
bool GetAll(DirectoryInfo dir, ref List <CustFileInfo> fileList) { FileInfo[] allFile = dir.GetFiles(); foreach (FileInfo fi in allFile) { string path = fi.FullName; bool bExist = false; if (_shareFileDict[_curPath] != null) { bExist = _shareFileDict[_curPath].Exists(T => T.File.FullName == path); } if (!bExist) { string hash = null; List <string> hashList = null; // CheckIdle(_activePercent, _sleepTime); try { ShareFileEvent sfEvent = new ShareFileEvent(); sfEvent.FileName = fi.Name; sfEvent.Path = fi.FullName; if (Hashing != null) { Hashing(this, sfEvent); } hash = HashHelper.ComputeFileMd5(path, out hashList); //hashList = ComputeSHA1ByParts(path, _partSize); //SHA1 sha1 = SHA1.Create(); //StringBuilder hashBuild = new StringBuilder(); //foreach (var item in hashList) //{ // hashBuild.Append(item); //} //byte[] hashBuf = sha1.ComputeHash(Encoding.UTF8.GetBytes(hashBuild.ToString())); //hashBuild.Clear(); //foreach (var item in hashBuf) //{ // hashBuild.Append(item.ToString("x2")); //} //hash = hashBuild.ToString(); if (HashComplete != null) { HashComplete(this, sfEvent); } } catch (Exception e) { Console.WriteLine(e); } if (hash != null && hash != string.Empty) { fileList.Add(new CustFileInfo() { File = fi, Hash = hash, HashList = hashList, Size = fi.Length, IsFolder = false }); } } } DirectoryInfo[] allDir = dir.GetDirectories(); foreach (DirectoryInfo item in allDir) { string path = item.FullName; if (_shareFileDict[_curPath] == null || !_shareFileDict[_curPath].Exists(T => T.File.FullName == path)) { string hash = HashHelper.ComputeStringMd5(_diskSerial + item.FullName); fileList.Add(new CustFileInfo() { File = item, Hash = hash, IsFolder = true }); } GetAll(item, ref fileList); } return(true); }