private static string CalculateFullHash(FileInformationItem file)
        {
            byte[] buffer = new byte[4 * 1024];
            byte[] runningHash = null;

            using (FileStream fs = new FileStream(file.FullPath, FileMode.Open))
            {

                int read = fs.Read(buffer, 0, buffer.Length);
                while (0 < read)
                {
                    runningHash = null == runningHash ? CalculateRawHash(buffer, 0, read) : CalculateRawHash(Merge(runningHash, buffer), 0, runningHash.Length + read);
                    read = fs.Read(buffer, 0, buffer.Length);
                }
            }

            return CalculateHash(buffer, 0, buffer.Length);
        }
 public static string CalculateHash(FileInformationItem file, HashMethod method)
 {
     if (HashMethod.Simple == method)
     {
         return CalculateSimpleHash(file);
     }
     else if (HashMethod.Lazy == method)
     {
         return CalculateLazyHash(file);
     }
     else if (HashMethod.Full == method)
     {
         return CalculateFullHash(file);
     }
     else
     {
         throw new ArgumentException();
     }
 }
        private static string CalculateLazyHash(FileInformationItem file)
        {
            try
            {
                byte[] buffer = new byte[1024 * 8];

                using (FileStream fs = new FileStream(file.FullPath, FileMode.Open))
                {
                    int read = fs.Read(buffer, 0, buffer.Length);

                    return CalculateHash(buffer, 0, read);
                }
            }
            catch (UnauthorizedAccessException)
            {
                return CalculateSimpleHash(file);
            }
            catch (IOException)
            {
                return CalculateSimpleHash(file);
            }
        }
 private static string CalculateSimpleHash(FileInformationItem file)
 {
     return CalculateHash(file.LastChange.ToString() + file.Size.ToString());
 }
        private static string GetSingleLine(FileInformationItem item)
        {
            List<string> fields = new List<string>();

            fields.Add(EscapeField(item.FileName));
            fields.Add(EscapeField(item.Extension));
            fields.Add(EscapeField(item.Hash));
            fields.Add(EscapeField(item.FullPath));
            fields.Add(EscapeField(item.Directory));
            fields.Add(EscapeField(item.Size.ToString()));
            fields.Add(EscapeField(item.Created.ToShortDateString()));
            fields.Add(EscapeField(item.LastChange.ToShortDateString()));
            fields.Add(EscapeField(item.LastOpen.ToShortDateString()));
            fields.Add(EscapeField(ResolveBoolean(item.Hidden).ToString()));
            fields.Add(EscapeField(ResolveBoolean(item.ReadOnly).ToString()));

            return string.Join(";", fields.ToArray());
        }
示例#6
0
        private void Scan(DirectoryInfo root)
        {
            FileInfo[] files = null;
            try
            {
                files = root.GetFiles("*");
            }
            catch (UnauthorizedAccessException)
            {
                files = new FileInfo[0];
            }

            foreach (FileInfo file in files)
            {
                if (!this.shouldRun)
                {
                    return;
                }

                if ((file.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint && (file.Attributes & FileAttributes.System) != FileAttributes.System)
                {
                    FileInformationItem item = new FileInformationItem(file);
                    item.Hash = HashFunctions.CalculateHash(item, this.hashMethod);
                    this.items.Add(item);
                }
            }

            DirectoryInfo[] directories = null;
            try
            {
                directories = root.GetDirectories();
            }
            catch (UnauthorizedAccessException)
            {
                directories = new DirectoryInfo[0];
            }

            foreach (DirectoryInfo newRoot in directories)
            {
                Scan(newRoot);
            }
        }