Exemplo n.º 1
0
        // Method used to save the content to the json file.
        public static void Save()
        {
            // if the appdata dir does not exist create it
            DirectoryUtil.CreateDirectory(Configuration.APPDATA_PATH);

            // serialize the content to the json format
            // write it as a file into the appdata path
            File.WriteAllText(Path.Combine(Configuration.APPDATA_PATH, Configuration.JSON_NAME), JsonConvert.SerializeObject(ConfigurationJsonModel));

            LoggingUtil.Info("Configuration file has been saved.");
        }
Exemplo n.º 2
0
        // Append each log event into the log file specified
        // NOTE: we delete the log file each time the program starts
        // the code for deletion is within the App.xaml code
        private static void Writer(string type, string message)
        {
            if (Configuration.LOG_ENABLED == true)
            {
                // if the appdata dir does not exist create it
                DirectoryUtil.CreateDirectory(Configuration.APPDATA_PATH);

                using (StreamWriter streamWriter = File.AppendText(Path.Combine(Configuration.APPDATA_PATH, Configuration.LOG_NAME)))
                {
                    streamWriter.WriteLine(DateTime.Now.ToString("d/M/yyyy HH:mm:ss ") + type + message);
                }
            }
        }
Exemplo n.º 3
0
        // Method for compressing files.
        public static void Compress(FileInfo fileToCompress, string baseFolder)
        {
            try
            {
                // open the file as a file stream
                // that need to be compressed
                using (FileStream fileStream = fileToCompress.OpenRead())
                {
                    // if the file is not hidden and has not the
                    // compression file extension (e.g. ".gz")
                    // go on and compress the file
                    if (!File.GetAttributes(fileToCompress.FullName).HasFlag(FileAttributes.Hidden) & !fileToCompress.DirectoryName.Contains(Configuration.TEMP_FOLDER_NAME))
                    {
                        // create a temporary directory if it does not exist
                        DirectoryUtil.CreateDirectory(Path.Combine(baseFolder, Configuration.TEMP_FOLDER_NAME));

                        // path for the dictionary key
                        string key = fileToCompress.FullName.Replace(baseFolder, "");

                        // generate the md5 of the file
                        string hashValue = CryptographyUtil.Md5(File.ReadAllBytes(fileToCompress.FullName));

                        // create the sub-directory in the temp folder
                        // if it does not exist
                        DirectoryUtil.CreateDirectory(Path.Combine(baseFolder, Configuration.TEMP_FOLDER_NAME + fileToCompress.FullName.Replace(baseFolder, "").Replace(fileToCompress.Name, "")));

                        // create the file in the location of the base
                        // folder + temp folder + the file name, again we
                        // need to replace the base path to append it after the
                        // temp folder in order to prevent an absolute path at the end
                        // append the compressed file extension
                        using (FileStream compressedFileStream = File.Create(Path.Combine(baseFolder, Configuration.TEMP_FOLDER_NAME + fileToCompress.FullName.Replace(baseFolder, "") + Configuration.COMPRESSION_FORMAT)))
                        {
                            // compress the file
                            using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                            {
                                // copy the file of the
                                // stream to the specified location
                                fileStream.CopyTo(compressionStream);
                                LoggingUtil.Info(String.Format("Compressed: {0}", fileToCompress.FullName));
                            }

                            // just some not explicit needed information
                            // reduced size info after the compression ...
                            FileInfo fileInfo = new FileInfo(Path.Combine(baseFolder, Configuration.TEMP_FOLDER_NAME + fileToCompress.FullName.Replace(baseFolder, "") + Configuration.COMPRESSION_FORMAT));
                            Console.WriteLine("Compressed {0} from {1} to {2} bytes.", fileToCompress.Name, fileToCompress.Length.ToString(), fileInfo.Length.ToString());
                        }

                        // Add to the list each item (file) with the specified values
                        checksumElements.Add(new FileJsonModel()
                        {
                            Name     = fileToCompress.Name,
                            Path     = key,
                            Hash     = hashValue,
                            Date     = fileToCompress.CreationTimeUtc.ToUniversalTime().ToString(),
                            Size     = fileToCompress.Length,
                            Language = null
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                // Log if the file could not be
                // compressed due to an exception.
                LoggingUtil.Exception(String.Format("Compress Exception: {0}", ex.Message));
            }
        }