Exemple #1
0
        private FileHash CalculateFileHash(string rootFolder, string path, Dictionary <uint, FileHash> checkSums)
        {
            CRC32 crc32 = new CRC32();

            // check if this is a folder
            if (Directory.Exists(path))
            {
                FileHash folder = new FileHash();
                folder.IsFolder = true;
                folder.Name     = Path.GetFileName(path);
                folder.FullName = path.Substring(rootFolder.Length - 1);

                // process child folders and files
                List <string> childFiles = new List <string>();
                childFiles.AddRange(Directory.GetDirectories(path));
                childFiles.AddRange(Directory.GetFiles(path));

                foreach (string childFile in childFiles)
                {
                    FileHash childHash = CalculateFileHash(rootFolder, childFile, checkSums);
                    folder.Files.Add(childHash);

                    // check sum
                    folder.CheckSum += childHash.CheckSum;
                    folder.CheckSum += ConvertCheckSumToInt(crc32.ComputeHash(Encoding.UTF8.GetBytes(childHash.Name)));

                    //Debug.WriteLine(folder.CheckSum + " : " + folder.FullName);
                }

                // move list to array
                folder.FilesArray = folder.Files.ToArray();

                if (!checkSums.ContainsKey(folder.CheckSum))
                {
                    checkSums.Add(folder.CheckSum, folder);
                }

                return(folder);
            }

            FileHash file = new FileHash();

            file.Name     = Path.GetFileName(path);
            file.FullName = path.Substring(rootFolder.Length - 1);

            // calculate CRC32
            using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                file.CheckSum = ConvertCheckSumToInt(
                    crc32.ComputeHash(fs));
            }

            if (!checkSums.ContainsKey(file.CheckSum))
            {
                checkSums.Add(file.CheckSum, file);
            }

            //Debug.WriteLine(file.CheckSum + " : " + file.FullName);

            return(file);
        }
Exemple #2
0
        private FileHash CalculateFileHash(string rootFolder, string path, Dictionary<uint, FileHash> checkSums)
        {
            CRC32 crc32 = new CRC32();

            // check if this is a folder
            if (Directory.Exists(path))
            {
                FileHash folder = new FileHash();
                folder.IsFolder = true;
                folder.Name = Path.GetFileName(path);
                folder.FullName = path.Substring(rootFolder.Length - 1);

                // process child folders and files
                List<string> childFiles = new List<string>();
                childFiles.AddRange(Directory.GetDirectories(path));
                childFiles.AddRange(Directory.GetFiles(path));

                foreach (string childFile in childFiles)
                {
                    FileHash childHash = CalculateFileHash(rootFolder, childFile, checkSums);
                    folder.Files.Add(childHash);

                    // check sum
                    folder.CheckSum += childHash.CheckSum;
                    folder.CheckSum += ConvertCheckSumToInt(crc32.ComputeHash(Encoding.UTF8.GetBytes(childHash.Name)));

                    //Debug.WriteLine(folder.CheckSum + " : " + folder.FullName);
                }

                // move list to array
                folder.FilesArray = folder.Files.ToArray();

                if (!checkSums.ContainsKey(folder.CheckSum))
                    checkSums.Add(folder.CheckSum, folder);

                return folder;
            }

            FileHash file = new FileHash();
            file.Name = Path.GetFileName(path);
            file.FullName = path.Substring(rootFolder.Length - 1);

            // calculate CRC32
			using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				file.CheckSum = ConvertCheckSumToInt(
					crc32.ComputeHash(fs));
			}

            if (!checkSums.ContainsKey(file.CheckSum))
                checkSums.Add(file.CheckSum, file);

            //Debug.WriteLine(file.CheckSum + " : " + file.FullName);

            return file;
        }