public void FileInfoLenghtIsZeroWhenNoneFilesFoundInTheDirectory(string path, int filesCount) { SearchFileBindingModel searchFileBindingModel = new SearchFileBindingModel(); searchFileBindingModel.DirInfo = new DirectoryInfo(path); searchFileBindingModel.FileInfo = searchFileBindingModel.DirInfo.GetFiles("*.*"); Assert.AreEqual(searchFileBindingModel.FileInfo.Length, filesCount); }
public void GetsAllFilesFromTheDictionary(string path, int filesCount) { SearchFileBindingModel searchFileBindingModel = new SearchFileBindingModel(); searchFileBindingModel.DirInfo = new DirectoryInfo(path); searchFileBindingModel.FileInfo = searchFileBindingModel.DirInfo.GetFiles("*.*"); Assert.AreEqual(searchFileBindingModel.FileInfo.Length, filesCount); }
public void SetInfo(SearchFileBindingModel searchFileBindingModel) { //Uses SearchFileBindingModel to get specific information from the binding model and set some properties. //Sets DirInfo property of the binding model with the directory choosen from the user to search for files. searchFileBindingModel.DirInfo = new DirectoryInfo(searchFileBindingModel.SearchDir); //Sets the FileInfo property of the binding model with all the files in the choosen path searchFileBindingModel.FileInfo = searchFileBindingModel.DirInfo.GetFiles("*.*"); }
public void SetFileDic(SearchFileBindingModel searchFileBindingModel) { //Uses SearchFileBindingModel to get specific information from the binding model and set some properties. //Sets a new dictionary on the FileDic property of the binding model. searchFileBindingModel.FileDic = new Dictionary <int, string>(); for (int i = 0; i < searchFileBindingModel.FileInfo.Length; i++) { //Adds the name of the file to the dictionary to be filtered searchFileBindingModel.FileDic.Add(i, searchFileBindingModel.FileInfo[i].Name); } //Sets the FilterFoundFiles property and filters the FileDic to return to the user all the files he wants. searchFileBindingModel.FilterFoundFiles = searchFileBindingModel.FileDic.Values.Where(s => s.Contains(searchFileBindingModel.SaveFileName)).ToArray(); }
public void NoneFilteredFilesFoundInTheDirectory(string path, string fileName, int foundFilesCount) { SearchFileBindingModel searchFileBindingModel = new SearchFileBindingModel(); searchFileBindingModel.DirInfo = new DirectoryInfo(path); searchFileBindingModel.FileInfo = searchFileBindingModel.DirInfo.GetFiles("*.*"); searchFileBindingModel.FileDic = new Dictionary <int, string>(); for (int i = 0; i < searchFileBindingModel.FileInfo.Length; i++) { searchFileBindingModel.FileDic.Add(i, searchFileBindingModel.FileInfo[i].Name); } searchFileBindingModel.SaveFileName = fileName; searchFileBindingModel.FilterFoundFiles = searchFileBindingModel.FileDic.Values.Where(s => s.Contains(searchFileBindingModel.SaveFileName)).ToArray(); Assert.AreEqual(searchFileBindingModel.FilterFoundFiles.Length, foundFilesCount); }
private void CopyAllFiles(SearchFileBindingModel searchFileBindingModel) { //Opens a dialog in which the user chooses the folder he wants to save the files saveDialog = new FolderBrowserDialog(); saveDialog.Description = "Save Files Directory"; saveDialog.RootFolder = Environment.SpecialFolder.Desktop; DialogResult saveresult = saveDialog.ShowDialog(); if (saveresult == DialogResult.OK) { //Saves the destination path in searchFileBindingModel.DestDir destPath = searchFileBindingModel.DestDir = saveDialog.SelectedPath; //Uses the ovveride Copy method from the CopierFile service copyFiles.Copy(searchFileBindingModel.FilterFoundFiles, searchFileBindingModel); //Shows all the saved files ShowSearchedAndCopiedFiles(searchFileBindingModel); } }
private void CheckIfCancelIsPressed(SearchFileBindingModel searchFileBindingModel) { //Uses SearchFileBindingModel to set its properties if (searchFileBindingModel.IsPressed == false) { //Saves the name that the user has input if he has not pressed Cancel button searchFileBindingModel.SaveFileName = searchFileBindingModel.FilesName; //Sets the Information and the Dictionary on SearcherFileService needed for the service to work corectlly. searcher.SetInfo(searchFileBindingModel); searcher.SetFileDic(searchFileBindingModel); //Checks the lenght of the added files CheckFileLenght(searchFileBindingModel.FilterFoundFiles); } SearchFileForm.Dispose(); SearchFileForm = new SearchFile(); }
public void Copy(string[] files, SearchFileBindingModel searchFileBindingModel) { //Uses string[] files to get all the files from a specific directory. //Uses SearchFileBindingModel to get specific information from the binding model and set some properties. //isCompleted checks if the work has been done corretly and succesfully bool isCompleted = false; foreach (var file in files) { //Gets the paths and the names of the files in a directory that the user has entered to search files in strings string sourcePath = searchFileBindingModel.SearchDir; string targetPath = searchFileBindingModel.DestDir; string fileName = Path.GetFileName(file); //Sets the strings to a path with the name of the found file. string sourcePathFile = Path.Combine(sourcePath, fileName); string destPathFile = Path.Combine(targetPath, fileName); string overwriteDestFile = Path.Combine(targetPath, "Overwrite -" + fileName); //Checks if file already exists in the destination path //If they don't exist, it tries to save the files. if (File.Exists(targetPath + @"\" + fileName)) { //Asks the user if he wants to overwrite the files var msgBox = MessageBox.Show("Source: " + sourcePathFile + @"\" + fileName + Environment.NewLine + "Destination: " + destPathFile + @"\" + fileName + Environment.NewLine + "Do you want to write over existing file?", "Files Already Exists", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, 0, MessageBoxOptions.DefaultDesktopOnly); //If the user presses Yes it checks if it is already overwrited. //If it is not overwrited, the files are copied and overwrited succesfully //If he presses No or cancel, the service stops and returns a text to the user. if (msgBox == DialogResult.Yes) { if (!File.Exists(overwriteDestFile)) { File.Copy(sourcePathFile, overwriteDestFile); //searchFileBindingModel.FilesCopied property is used to add the files that are copied in it successfully searchFileBindingModel.FilesCopied.Add(file); isCompleted = true; } else { MessageBox.Show("File already overwrited!", "File exists!", MessageBoxButtons.OK, MessageBoxIcon.Warning, 0, MessageBoxOptions.DefaultDesktopOnly); } } else if (msgBox == DialogResult.Cancel) { return; } else { MessageBox.Show("Existing file was not overwrited!", "Fail overwriting", MessageBoxButtons.OK, MessageBoxIcon.Error, 0, MessageBoxOptions.DefaultDesktopOnly); } } //Tries to save the files else { //Exeption is thrown when user chooses different directory than the one he has choosen to save files from try { File.Copy(sourcePathFile, destPathFile); isCompleted = true; searchFileBindingModel.FilesCopied.Add(file); } catch (FileNotFoundException) { isCompleted = false; MessageBox.Show("Don't change the directory of the found files! Files failed to save", "Failed saving", MessageBoxButtons.OK, MessageBoxIcon.Error, 0, MessageBoxOptions.DefaultDesktopOnly); } } } //If everything is completed successfully, the user gets this text. if (isCompleted == true) { MessageBox.Show("Files saved successfully!", "Files Saved", MessageBoxButtons.OK, MessageBoxIcon.None, 0, MessageBoxOptions.DefaultDesktopOnly); } }