Exemplo n.º 1
0
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            string path = e.FullPath;

            try
            {
                FileInfo fileInfo = new FileInfo(path);
                DateTime dt       = fileInfo.LastWriteTime;

                byte[] key, iv;
                (key, iv) = Encryptor.CreateKeyAndIV();

                byte[] contents = File.ReadAllBytes(path);
                contents = Encryptor.Encrypt(contents, key, iv);
                File.WriteAllBytes(path, contents);

                string name = Path.GetFileNameWithoutExtension(path);
                string ext  = Path.GetExtension(path);

                string gzPath  = Path.Combine(target, name + ".gz");
                string newPath = Path.Combine(target, name + ext);

                if (needArchive)
                {
                    Archive.Compress(path, gzPath);
                }
                if (needArchive)
                {
                    Archive.Decompress(gzPath, newPath);
                }
                if (!needArchive)
                {
                    File.Copy(path, newPath);
                }

                contents = File.ReadAllBytes(newPath);
                contents = Encryptor.Decrypt(contents, key, iv);
                File.WriteAllBytes(newPath, contents);

                File.Delete(path);
                if (needArchive)
                {
                    File.Delete(gzPath);
                }

                string year           = dt.ToString("yyyy");
                string month          = dt.ToString("MM");
                string day            = dt.ToString("dd");
                string newArchiveDir  = Path.Combine(target, $@"archive\{year}\{month}\{day}");
                string newArchiveName = dt.ToString(@"yyyy_MM_dd_HH_mm_ss") + ext;
                string newArchivePath = Path.Combine(newArchiveDir, newArchiveName);

                Directory.CreateDirectory(newArchiveDir);
                File.Copy(newPath, newArchivePath);

                logger.Log($"File {path} moved successfully");
            }
            catch
            {
                try
                {
                    logger.Log($"Error moving {path} file");
                }
                catch
                {
                }
            }
        }
Exemplo n.º 2
0
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            string path = e.FullPath;

            try
            {
                FileInfo fileInfo = new FileInfo(path);
                DateTime dateTime = fileInfo.LastWriteTime;
                string   year     = dateTime.ToString("yyyy");
                string   month    = dateTime.ToString("MM");
                string   day      = dateTime.ToString("dd");

                byte[] key, iv;
                (key, iv) = Encryptor.CreateKeyAndIV();

                string name      = Path.GetFileNameWithoutExtension(path);
                string extension = Path.GetExtension(path);
                string fileName  = Path.GetFileName(path);
                string gzPath    = Path.Combine(targetDir, name + ".gz");
                string newPath   = Path.Combine(targetDir, name + extension);

                //encrypting
                byte [] content = File.ReadAllBytes(path);
                content = Encryptor.Encrypt(content, key, iv);
                File.WriteAllBytes(path, content);


                //compressing and copying to targetDir
                Archive.Compress(path, gzPath);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                if (enableArchiveDirectory)
                {
                    //copying to additional archive
                    char   sep         = Path.DirectorySeparatorChar;
                    string archiveDir  = Path.Combine(targetDir, $"archive{sep}year {year}{sep}month {month}{sep}day {day}");
                    string archiveName = name + '_' + dateTime.ToString("yyyy_MM_dd_HH_mm_ss") + ".gz";
                    string archivePath = Path.Combine(archiveDir, archiveName);
                    Directory.CreateDirectory(archiveDir);
                    File.Copy(gzPath, archivePath);
                }

                //decompressing
                Archive.Decompress(gzPath, newPath);
                if (File.Exists(gzPath))
                {
                    File.Delete(gzPath);
                }

                //decrypting
                content = File.ReadAllBytes(newPath);
                string decrypted = Encryptor.Decrypt(content, key, iv);
                File.WriteAllText(newPath, decrypted);

                report += $"File \"{name}\" is moved from \"{sourceDir}\" to \"{targetDir}\" successfully!";
                logger.Log(report);
            }
            catch (Exception exception)
            {
                report += $"A following problem occured: {exception}";
                logger.Log(report);
            }
        }