示例#1
0
 private bool DirectoryScanned(string path)
 {
     using (var context = new DFDataContext())
     {
         var entity = context.DFPaths.Where(x => x.Path == path).FirstOrDefault();
         if (entity == null)
         {
             return(false);
         }
         else
         {
             if (entity.ScanFinished != null)
             {
                 return(true);
             }
             else
             {
                 var fileEntities = context.DFHashes.Where(x => x.PathId == entity.Id).ToList();
                 context.DFHashes.RemoveRange(fileEntities);
                 context.DFPaths.Remove(entity);
                 context.SaveChanges();
                 return(false);
             }
         }
     }
 }
示例#2
0
        public void ScanFiles4Directory(string dir, string searchPattern, decimal dirId, int maxDegreeOfParallelism)
        {
            var files      = Directory.GetFiles(dir, searchPattern);
            var dfHashList = new ConcurrentBag <DFHash>();

            Parallel.ForEach(files, new ParallelOptions {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
            }, item =>
            {
                using (var fs = new FileStream(item, FileMode.Open, FileAccess.Read))
                {
                    var entity = new DFHash()
                    {
                        SHA1     = BitConverter.ToString(SHA1.Create().ComputeHash(fs)),
                        PathId   = dirId,
                        FileSize = fs.Length,
                        FileName = item
                    };
                    dfHashList.Add(entity);
                }
            });
            using (var context = new DFDataContext())
            {
                context.DFHashes.AddRange(dfHashList);
                context.SaveChanges();
            }

            UpdateDbPath(dirId, files.Length);

            Logger.Info($"{dir} is scanned.");
        }
示例#3
0
 public void RemoveAllData()
 {
     using (var context = new DFDataContext())
     {
         context.Database.ExecuteSqlCommand("TRUNCATE TABLE DFHashes");
         context.Database.ExecuteSqlCommand("TRUNCATE TABLE DFPaths");
         context.SaveChanges();
     }
 }
示例#4
0
 private void UpdateDbPath(decimal id, int fileCount)
 {
     using (var context = new DFDataContext())
     {
         var entity = context.DFPaths.Where(x => x.Id == id).FirstOrDefault();
         entity.FileCount    = fileCount;
         entity.ScanFinished = DateTime.Now;
         context.SaveChanges();
     }
 }
示例#5
0
 private decimal CreateDirectoryInDb(string path)
 {
     using (var context = new DFDataContext())
     {
         var entity = new DFPath
         {
             Path        = path,
             FileCount   = 0,
             ScanStarted = DateTime.Now
         };
         context.DFPaths.Add(entity);
         context.SaveChanges();
         return(entity.Id);
     }
 }