Esempio n. 1
0
        // Пока что полностью не нужные методы ...
        public static void CheckSourceDir(string sourceDirPath, string targetDirectoryPath)
        {
            List <string> existedFiles;
            List <string> newFiles;

            //List<string> newCompressedFiles; // Stores names of files after compressing.

            try
            {
                existedFiles = new List <string>();
                newFiles     = new List <string>();
                //newCompressedFiles = new List<string>();

                DirectoryInfo dirInfo = new DirectoryInfo(sourceDirPath);

                ProcessDirectoriesForNewFiles(dirInfo, newFiles, existedFiles);

                // Processing all new files.
                foreach (var newFile in newFiles)
                {
                    // Encryption logic....
                    var encryptedFileName = Encrypt(newFile);

                    // Compress...
                    Compress(encryptedFileName);
                    //newCompressedFiles.Add(GetZipArchiveName(encryptedFileName));

                    // Move archive file to "TargetDirectory"...
                    var movedFile = MoveFileOrFolder(encryptedFileName, targetDirectoryPath);

                    // Decompress...
                    DecompressGZip(movedFile);

                    // DeEncryption...
                    Decrypt(movedFile);
                }

                // Update existed files.
                existedFiles.AddRange(newFiles);
            }
            catch (Exception ex)
            {
                UserInteraction.ErrorMsg();
                UserInteraction.PrintExceptionInfo(ex);
            }

            WriteLine();
        }
Esempio n. 2
0
        // Write text to the end of file (doesn't rewrite it).
        public static void WriteToFile(string fileName)
        {
            FileInfo file;

            try
            {
                file = new FileInfo(fileName);
            }
            catch (Exception ex)
            {
                UserInteraction.PrintExceptionInfo(ex);
                WriteLine();
                return;
            }

            if (!file.Exists)
            {
                UserInteraction.ErrorMsg();
                WriteLine($"File {fileName} doesn't exists !");
            }
            else if (file.Extension != ".txt")
            {
                UserInteraction.ErrorMsg();
                WriteLine($"File {fileName} should be an .txt file !");
            }
            else
            {
                Write("Input text (for writing to file) : ");
                var userText = ReadLine();

                try
                {
                    using (StreamWriter sw = new StreamWriter(fileName, true, System.Text.Encoding.Default))
                    {
                        sw.WriteLineAsync(userText);
                    }

                    UserInteraction.SuccessMsg();
                    WriteLine("Recording completed !");
                }
                catch (Exception ex)
                {
                    UserInteraction.PrintExceptionInfo(ex);
                }
            }

            WriteLine();
        }
Esempio n. 3
0
        public static void ReadTextFromFile(string filePath)
        {
            string   stringFromFile = string.Empty;
            FileInfo file;

            try
            {
                file = new FileInfo(filePath);
            }
            catch (Exception ex)
            {
                UserInteraction.ErrorMsg();
                UserInteraction.PrintExceptionInfo(ex);
                WriteLine();
                return;
            }

            if (!file.Exists)
            {
                UserInteraction.ErrorMsg();
                UserInteraction.FileNotExistsMsg(file.FullName);
            }
            else
            {
                try
                {
                    using (StreamReader sr = new StreamReader(file.FullName))
                    {
                        stringFromFile = sr.ReadToEnd();
                    }

                    UserInteraction.SuccessMsg();
                    WriteLine("Text from file : ");
                    WriteLine(stringFromFile);
                }
                catch (Exception ex)
                {
                    UserInteraction.ErrorMsg();
                    UserInteraction.PrintExceptionInfo(ex);
                }
            }

            WriteLine();
        }
Esempio n. 4
0
        // Returns new file name.
        public static string MoveFileOrFolder(string currentDirPath, string newDirPath)
        {
            var supposedNewPath = string.Empty;

            try
            {
                supposedNewPath = Path.Combine(newDirPath, GetFileOrFolderNameFromPath(currentDirPath));
            }
            catch (Exception ex)
            {
                UserInteraction.PrintExceptionInfo(ex);
                return(supposedNewPath);
            }

            if (!File.Exists(currentDirPath) && !Directory.Exists(currentDirPath))
            {
                WriteLine($"Such file or folder \"{currentDirPath}\" doesn't exists ! ");
            }
            else if (!Directory.Exists(newDirPath))
            {
                WriteLine($"Such file or folder \"{newDirPath}\" doesn't exists ! ");
            }
            else if (File.Exists(supposedNewPath) || Directory.Exists(supposedNewPath))
            {
                WriteLine($"Such file or directory \"{supposedNewPath}\" already exists ! ");
            }
            else
            {
                try
                {
                    Directory.Move(currentDirPath, supposedNewPath);
                    WriteLine("Move successfully ! ");
                }
                catch (Exception ex)
                {
                    UserInteraction.PrintExceptionInfo(ex);
                }
            }

            WriteLine();

            return(supposedNewPath);
        }
Esempio n. 5
0
        public static string GetFileOrFolderName()
        {
            string name;

            while (true)
            {
                name = ReadLine();

                if (GoodFileOrFolderName(name))
                {
                    break;
                }
                else
                {
                    UserInteraction.InvalidInputMessage();
                }
            }

            return(name);
        }
Esempio n. 6
0
 public static void DeleteDirectory(string path)
 {
     if (Directory.Exists(path))
     {
         try
         {
             Directory.Delete(path);
             WriteLine($"Directory \"{path}\" was successfully deleted ! \n");
         }
         catch (Exception ex)
         {
             UserInteraction.PrintExceptionInfo(ex);
             return;
         }
     }
     else
     {
         WriteLine($"Folder hasn't been deleted !");
         WriteLine($"Folder \"{path}\" doesn't exists ! \n");
     }
 }
Esempio n. 7
0
 public static void DeleteFile(string path)
 {
     if (File.Exists(path))
     {
         try
         {
             File.Delete(path);
             WriteLine($"File \"{path}\" was successfully deleted ! \n");
         }
         catch (Exception ex)
         {
             UserInteraction.PrintExceptionInfo(ex);
             return;
         }
     }
     else
     {
         WriteLine($"File hasn't been deleted !");
         WriteLine($"File \"{path}\" doesn't exists ! \n");
     }
 }
Esempio n. 8
0
        // Path of compressed file or folder.
        // Typically : Path.ChangeExtension(dirPath, ".zip").
        // dirPath - source filepath.
        public static void DecompressZip(string path)
        {
            string directoryOfPath          = GetDirectoryFromFileOrFolderPath(path);
            string fileOrFolderNameFromPath = GetFileOrFolderNameFromPath(path);
            string newFileOrFolderNameFromPath;
            string destinationPath;

            try
            {
                newFileOrFolderNameFromPath = Path.ChangeExtension(fileOrFolderNameFromPath, null);
                destinationPath             = Path.Combine(directoryOfPath, newFileOrFolderNameFromPath);

                if (Directory.Exists(destinationPath) || File.Exists(destinationPath))
                {
                    throw new Exception($"Such file or directory {destinationPath} is already exists !");
                }
                else
                {
                    if (File.Exists(path))
                    {
                        Directory.CreateDirectory(destinationPath);
                        ZipFile.ExtractToDirectory(path, destinationPath);
                    }
                    else
                    {
                        throw new Exception($"Such Directory or file {path} doesn't exists ! ");
                    }
                }

                UserInteraction.SuccessMsg();
                WriteLine($"Decompressed file : {destinationPath} !");
            }
            catch (Exception ex)
            {
                UserInteraction.ErrorMsg();
                UserInteraction.PrintExceptionInfo(ex);
            }

            WriteLine();
        }
Esempio n. 9
0
        // Renames according pattern - "Sales_YYYY_MM_DD_HH_mm_SS.txt"
        public static string RenameBuisnessFile(string sourceFilePath)
        {
            string childDirectory = Path.GetDirectoryName(sourceFilePath);
            string newFilePath    = string.Empty;

            try
            {
                FileInfo ourFile      = new FileInfo(sourceFilePath);
                DateTime creationTime = ourFile.CreationTime;

                string newFileName = $"Q_{creationTime.Year}_{creationTime.Month}_{creationTime.Day}_{creationTime.Hour}_" +
                                     $"{creationTime.Minute}_{creationTime.Second}.txt";

                // Строим директорию относительно вермени создания файла.
                childDirectory = Path.Combine(childDirectory, $"{creationTime.Year}");
                childDirectory = Path.Combine(childDirectory, $"{creationTime.Month}");
                childDirectory = Path.Combine(childDirectory, $"{creationTime.Day}");

                if (!Directory.Exists(childDirectory))
                {
                    Directory.CreateDirectory(childDirectory);
                }

                newFilePath = Path.Combine(childDirectory, newFileName);

                // Chenges name of file if need.
                newFilePath = GetUniqueFileName(newFilePath);

                RenameFile(sourceFilePath, newFilePath);
            }
            catch (Exception ex)
            {
                UserInteraction.ErrorMsg();
                UserInteraction.PrintExceptionInfo(ex);
            }

            return(newFilePath);
        }
Esempio n. 10
0
        private static string[] GetAllDirectories()
        {
            List <string> ourDirectories = new List <string>();

            try
            {
                DriveInfo[] drives = DriveInfo.GetDrives();

                foreach (DriveInfo drive in drives)
                {
                    var partOfDirectories = GetAllDirectories(drive.Name);

                    // Add our "partOfFiles" list to the original "ourFiles" list.
                    ourDirectories.AddRange(partOfDirectories);
                }
            }
            catch (Exception ex)
            {
                UserInteraction.PrintExceptionInfo(ex);
            }

            return(ourDirectories.ToArray());
        }
Esempio n. 11
0
        private static bool GoodPath(string path)
        {
            bool niceInput = true;

            char[] badNameChars = { '/', ':', '*', '?', '<', '>', '"', '|' };
            char[] badDiskChars = { '/', '\\', '*', '?', '<', '>', '"', '|' };

            // Stores path in convinient format.
            // [0] - drive name, [1] - ":", [2] - "\", [3] - name1, [4] - "\", [5] - name2 ..
            List <string> separatedPath = new List <string>();

            for (int i = 0; i < path.Length; i++)
            {
                separatedPath.Add("");
            }

            for (int i = 0, j = 0; i < path.Length; i++)
            {
                if (j == 0)
                {
                    if (path[i] == ':')
                    {
                        if (separatedPath[j].Length == 0 || !DriveExists(separatedPath[j]))
                        {
                            niceInput = false;
                            break;
                        }
                        else
                        {
                            j++;
                            separatedPath[j] = ":";
                            j++;
                        }
                    }
                    else
                    {
                        // Check if path[i] char is correct.
                        foreach (var ourChar in badDiskChars)
                        {
                            if (ourChar == path[i])
                            {
                                niceInput = false;
                                break;
                            }
                        }
                        separatedPath[j] += path[i];
                    }
                }
                else if (j % 2 == 0)
                {
                    if (path[i] == '\\')
                    {
                        separatedPath[j] = "\\";
                        j++;
                    }
                    else
                    {
                        niceInput = false;
                        break;
                    }
                }
                else // Here we checks for correct name.
                {
                    if (path[i] == '\\')
                    {
                        if (separatedPath[j].Length == 0)
                        {
                            niceInput = false;
                            break;
                        }
                        else
                        {
                            j++;
                            separatedPath[j] = path[i].ToString();
                            j++;
                        }
                    }
                    else
                    {
                        foreach (var ourChar in badNameChars)
                        {
                            if (path[i] == ourChar)
                            {
                                UserInteraction.InvalidInputMessage();
                                break;
                            }
                        }
                        separatedPath[j] += path[i];
                    }
                }
            } // End for.

            return(niceInput);
        }