private void CheckIfCancelIsPressed(RenameFileBindingModel renameFileBindingModel)
        {
            //Uses RenameFileBindingModel to set its properties

            if (renameFileBindingModel.IsPressed == false)
            {
                //Uses the Rename method from the RenamerFile service
                renameFileService.Rename(renameFileBindingModel);
                //Checks if file is renamed successfully
                if (renameFileBindingModel.IsRenamed == true)
                {
                    //If it is renamed successfully it sets the variables and returns them.
                    string oldFile = renameFileBindingModel.FileName.Split('\\').Last();
                    oldName = $"File {oldFile} renamed to:";
                    newName = renameFileBindingModel.NewName + Path.GetExtension(renameFileBindingModel.FileName);
                }
                else
                {
                    //If it is not renamed successfully it returns this.
                    oldName = "File was not renamed";
                }
            }

            RenameFileForm.Dispose();
            RenameFileForm = new RenameFile();
        }
Ejemplo n.º 2
0
        public void RenameMethodOverwritesFilesSuccesfully()
        {
            CurrentUser.user.username = "******";
            RenameFileBindingModel bindingModel = new RenameFileBindingModel();

            bindingModel.FileName = @"C:\Users\Nikih\Desktop\Resources\banan.docx";
            bindingModel.NewName  = "banan";
            RenamerFileService renameFileService = new RenamerFileService();

            renameFileService.Rename(bindingModel);

            FileAssert.Exists(@"C:\Users\Nikih\Desktop\Resources\Overwrite -banan.docx");
        }
Ejemplo n.º 3
0
        public void RenameMethodRenamesFilesCorrectlly()
        {
            CurrentUser.user.username = "******";
            RenameFileBindingModel bindingModel = new RenameFileBindingModel();

            bindingModel.FileName = @"C:\Users\Nikih\Desktop\Resources\banan.docx";
            bindingModel.NewName  = "izrod";
            RenamerFileService renameFileService = new RenamerFileService();

            renameFileService.Rename(bindingModel);

            FileAssert.Exists(@"C:\Users\Nikih\Desktop\Resources\izrod.docx");
        }
Ejemplo n.º 4
0
        public void RenameMethodFailed()
        {
            CurrentUser.user.username = "******";
            RenameFileBindingModel bindingModel = new RenameFileBindingModel();

            bindingModel.FileName = @"C:\Users\Nikih\Desktop\Resources\banan.docx";
            bindingModel.NewName  = "ggg";
            RenamerFileService renameFileService = new RenamerFileService();

            FieldInfo[] fields    = typeof(RenamerFileService).GetFields(BindingFlags.Instance | BindingFlags.NonPublic).ToArray();
            FieldInfo   isRenamed = fields.FirstOrDefault(x => x.Name == "isRenamed");

            isRenamed.SetValue(renameFileService, false);

            renameFileService.Rename(bindingModel);

            Assert.IsFalse((bool)isRenamed.GetValue(renameFileService));
        }
 private void CheckUserInput(RenameFileBindingModel renameFileBindingModel)
 {
     //Checks if user has pressed OK for saving the source directory
     if (result == DialogResult.OK)
     {
         //Saves the old file name in renameFileBindingModel.FileName
         renameFileBindingModel.FileName = renameFileDialog.FileName;
         sourcePath = Path.GetDirectoryName(renameFileBindingModel.FileName);
         //Shows the RenamerFileForm to the user.
         try
         {
             RenameFileForm.ShowDialog();
         }
         catch (InvalidOperationException)
         {
             MessageBox.Show("Please, enter a name for a file before trying to search file again", "Enter name",
                             MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 0,
                             MessageBoxOptions.DefaultDesktopOnly);
             return;
         }
         //Checks if Cancel is pressed in the SearchFileForm form.
         CheckIfCancelIsPressed(RenameFileForm.GetRenameFileBindingModel());
     }
 }
        private static void CheckIfTextBoxIsEmpty(string textBoxText, RenameFile RenameFileForm, RenameFileBindingModel bindingModel)
        {
            //uses string textBoxText to get user input for a name of a file
            //uses RenameFile Form to check if user has done everything properly on that form.
            //uses RenameFileBindingModel to set its properties.

            if (textBoxText != "")
            {
                SetRenameFileBindingModelNewName(textBoxText, bindingModel);
                RenameFileForm.Close();
            }
            else
            {
                ShowMessageBox();
            }
        }
        public static async void CheckIfTextBoxIsEmptyAsync(string textBoxText, RenameFile RenameFileForm, RenameFileBindingModel bindingModel)
        {
            //uses string textBoxText to get user input for a name of a file
            //uses RenameFile Form to check if user has done everything properly on that form.
            //uses RenameFileBindingModel to set its properties.

            if (textBoxText != "")
            {
                await Task.Run(() => SetRenameFileBindingModelNewName(textBoxText, bindingModel));

                RenameFileForm.Close();
            }
            else
            {
                await Task.Run(() => ShowMessageBox());
            }
        }
        public static void CheckIfUserPressesEnterNonAsync(string textBoxText, RenameFile RenameFileForm, KeyPressEventArgs e, RenameFileBindingModel bindingModel)
        {
            //uses string textBoxText to get user input for a  new name of a file
            //uses RenameFile Form, KeyPressEventArgs to check if user has done everything properly on that form.
            //uses RenameFileBindingModel to set its properties.

            if (e.KeyChar == (char)Keys.Enter)
            {
                //checks if user has pressed Enter in RenameFile Form
                CheckIfTextBoxIsEmpty(textBoxText, RenameFileForm, bindingModel);
            }
        }
 public static void SetRenameFileBindingModelIsPressedNonAsync(RenameFileBindingModel renameFileBindingModel)
 {
     SetRenameFileBindingModelIsPressed(renameFileBindingModel);
 }
 //Sets when user has pressed Cancel asynchronously on the RenameFile Form
 //Method is called in RenameFile Form to get the input of user
 public static async void SetRenameFileBindingModelIsPressedAsync(RenameFileBindingModel renameFileBindingModel)
 {
     await Task.Run(() => SetRenameFileBindingModelIsPressed(renameFileBindingModel));
 }
 //Sets when user has pressed Cancel
 //Method is called to be done asynchronously
 private static void SetRenameFileBindingModelIsPressed(RenameFileBindingModel renameFileBindingModel)
 {
     renameFileBindingModel.IsPressed = true;
 }
 //Sets the name of the file that the user wants to be renamed
 //Method is called to be done asynchronously
 private static void SetRenameFileBindingModelNewName(string textBoxText, RenameFileBindingModel renameFileBindingModel)
 {
     renameFileBindingModel.NewName = textBoxText;
 }
Ejemplo n.º 13
0
        public void Rename(RenameFileBindingModel renameFileBindingModel)
        {
            //Uses RenameFileBindingModel to get specific information from the binding model and set some properties.

            //isRenamed checks if the work has been done correctly and succesfully
            bool isRenamed = false;
            //strings that gets the name of the choosen file and get the new name for it
            string fileName = Path.GetFileName(renameFileBindingModel.FileName);
            string newName  = renameFileBindingModel.NewName;

            //Throws exeption if a file that should be renamed is opened while the service is trying to rename the files.
            try
            {
                //Gets the extension of the choosen file
                string extension = Path.GetExtension(renameFileBindingModel.FileName);
                //Gets the path of the file choosen to be renamed
                string sourcePath = Path.GetDirectoryName(renameFileBindingModel.FileName);
                string targetPath = Path.GetDirectoryName(renameFileBindingModel.FileName);

                //Sets the strings to a path with the name of the file that has to be renamed.
                string sourceFile = Path.Combine(sourcePath, fileName);
                //Sets the strings to a path with the New name of the file that has to be renamed + the extension of the file.
                string destFile          = Path.Combine(targetPath, newName + extension);
                string overwriteDestFile = Path.Combine(targetPath, "Overwrite -" + fileName);
                string overwriteFileName = Path.GetFileName("Overwrite -" + fileName);

                //Checks if file already exists in the destination path
                //If they don't exist, it tries to rename the file.
                if (File.Exists(destFile))
                {
                    //Asks the user if he wants to overwrite the file
                    var msgBox = MessageBox.Show("Source: " + sourceFile + @"\" + fileName + Environment.NewLine +
                                                 "Destination: " + destFile + @"\" + fileName + Environment.NewLine +
                                                 "Do you want to write over existing file?", "File 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 file is renamed 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.Move(sourceFile, overwriteDestFile);
                            //renameFileBindingModel.IsRenamed is used to tell that the file is renamed successfully.
                            renameFileBindingModel.IsRenamed = true;
                            isRenamed = true;
                            FileDatabaseServices.AddRenameOperation(fileName, newName, "File", true);
                        }
                        else
                        {
                            FileDatabaseServices.AddRenameOperation("Unsuccessfull operation", overwriteDestFile, "File", false);
                            MessageBox.Show("File already overwrited!", "File exists!",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning, 0,
                                            MessageBoxOptions.DefaultDesktopOnly);
                            return;
                        }
                    }
                    else if (msgBox == DialogResult.Cancel)
                    {
                        return;
                    }
                    else
                    {
                        FileDatabaseServices.AddRenameOperation("Unsuccessfull operation", overwriteDestFile, "File", false);
                        MessageBox.Show("Existing file was not renamed!", "Failed renaming",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error, 0,
                                        MessageBoxOptions.DefaultDesktopOnly);
                        return;
                    }
                }
                //Renames the file
                else
                {
                    File.Move(sourceFile, destFile);
                    renameFileBindingModel.IsRenamed = true;
                    isRenamed = true;
                    FileDatabaseServices.AddRenameOperation(fileName, newName + extension, "File", true);
                }
            }
            catch (IOException)
            {
                FileDatabaseServices.AddRenameOperation("Unsuccessfull operation", "None", "File", false);
                isRenamed = false;
                MessageBox.Show("File opened! \nPlease, close the file to proceed!", "Running process",
                                MessageBoxButtons.OK, MessageBoxIcon.Error, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
            //If everything is completed successfully, the user gets this text.
            if (isRenamed == true)
            {
                MessageBox.Show($"File renamed as {newName + Path.GetExtension(fileName)}", "File Renamed",
                                MessageBoxButtons.OK, MessageBoxIcon.None, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }