Пример #1
0
        private void AddFileInfo(CountedFileInfo info)
        {
            Action action = () => listBox.Items.Add(info.Filename + '_' + info.Sum + DateTime.Now.ToString("HH:mm:ss"));

            if (InvokeRequired)
            {
                Invoke(action);
            }
            else
            {
                action();
            }
        }
Пример #2
0
        public void StartWork(string filepath)
        {
            ulong byteSum = 0;

            using (FileStream rstream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader reader = new BinaryReader(rstream))
                {
                    //reading bytes to a preferred size byte array
                    byte[] buffer;
                    for (ulong i = 0; i < (ulong)rstream.Length; i += BUFFER_SIZE)
                    {
                        buffer = reader.ReadBytes(BUFFER_SIZE);

                        //check buffer sum and add it to byteSum
                        uint bufferSum = 0;
                        for (var index = 0; index < buffer.Length; index++)
                        {
                            var b = buffer[index];
                            bufferSum += b;
                        }
                        byteSum = byteSum + bufferSum;
                    }
                }
            }

            string fileName        = Path.GetFileName(filepath);
            string folderPath      = new FileInfo(filepath).DirectoryName;
            var    countedFileInfo = new CountedFileInfo()
            {
                Filename = fileName, Sum = byteSum
            };

            //making XML if not exists and add name+bytesum with CountedFileInfo with existed infos
            XmlSerializer formatter   = new XmlSerializer(typeof(CountedFileInfo[]));
            var           xmlFilePath = Path.Combine(folderPath, Properties.Settings.Default.summaryFileName);

            CountedFileInfo[] countedFiles = new CountedFileInfo[] { countedFileInfo };

            //locking access to file r/w for a moment
            lock (locker)
            {
                if (!File.Exists(xmlFilePath))
                {
                    using (FileStream fs = new FileStream(xmlFilePath, FileMode.Create))
                    {
                        //writing to XML
                        formatter.Serialize(fs, countedFiles);
                    }
                }
                else
                {
                    using (FileStream fs = new FileStream(xmlFilePath, FileMode.OpenOrCreate))
                    {
                        CountedFileInfo[] gotFromXMLcountedFiles = null;

                        try
                        {
                            gotFromXMLcountedFiles = (CountedFileInfo[])formatter.Deserialize(fs);
                        }
                        catch { }

                        if (gotFromXMLcountedFiles != null)
                        {
                            var foundDublicate = gotFromXMLcountedFiles.FirstOrDefault(t => t.Filename == fileName);
                            if (foundDublicate != null)
                            {
                                foundDublicate.Sum = countedFileInfo.Sum;
                                countedFiles       = gotFromXMLcountedFiles;
                            }
                            else
                            {
                                countedFiles = countedFiles.Union(gotFromXMLcountedFiles).ToArray();
                            }
                        }
                    }
                    using (FileStream fs = new FileStream(xmlFilePath, FileMode.Create))
                    {
                        //writing to XML
                        formatter.Serialize(fs, countedFiles);
                    }
                }

                //notification
                OnCount(countedFileInfo);
            }
        }
Пример #3
0
 private void Fileworker_OnCount(CountedFileInfo info)
 {
     WorkerOnCount(info);
 }