Пример #1
0
 new public string ToString()
 {
     return(String.Format("\nLeftPath: {0}\nRightPath: {1}\nLeftLine: {2}\nRightLine: {3}\nRefs: {4}",
                          (LeftPath?.ToString() ?? "null"),
                          (RightPath?.ToString() ?? "null"),
                          (LeftLine?.ToString() ?? "null"),
                          (RightLine?.ToString() ?? "null"),
                          Refs.ToString()));
 }
Пример #2
0
        private void UpdateFiles()
        {
            try
            {
                if (LeftPath.ToString() != "")
                {
                    DirectoryInfo   directoryInfo  = new DirectoryInfo(LeftPath.ToString());
                    DirectoryInfo[] subdirectories = directoryInfo.GetDirectories();

                    FileInfo[] files = directoryInfo.GetFiles();

                    var nameofdirectories = from dir in subdirectories
                                            select new Element {
                        Name = dir.Name, isElement = IsElement.IsDirectory, Date = dir.CreationTime.ToShortDateString()
                    };
                    var nameoffiles = from file in files
                                      select new Element {
                        Name = file.Name, isElement = IsElement.IsFile, Date = file.CreationTime.ToShortDateString()
                    };

                    var names = nameofdirectories.Union(nameoffiles);
                    Dispatcher.BeginInvoke((ThreadStart) delegate()
                    {
                        LeftList.ItemsSource = names;
                    });
                }

                if (RightPath.ToString() != "")
                {
                    DirectoryInfo   directoryInfo  = new DirectoryInfo(RightPath.ToString());
                    DirectoryInfo[] subdirectories = directoryInfo.GetDirectories();
                    FileInfo[]      files          = directoryInfo.GetFiles();

                    var nameofdirectories = from dir in subdirectories
                                            select new Element {
                        Name = dir.Name, isElement = IsElement.IsDirectory, Date = dir.CreationTime.ToShortDateString()
                    };
                    var nameoffiles = from file in files
                                      select new Element {
                        Name = file.Name, isElement = IsElement.IsFile, Date = file.CreationTime.ToShortDateString()
                    };

                    var names = nameofdirectories.Union(nameoffiles);
                    Dispatcher.BeginInvoke((ThreadStart) delegate()
                    {
                        RightList.ItemsSource = names;
                    });
                }
            }
            catch (System.UnauthorizedAccessException exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
Пример #3
0
        private void ChangePlace(bool delete, Element element, bool IsLeft)
        {
            try
            {
                string newpath, oldpath;
                string name = element.Name;
                if (IsLeft)
                {
                    if (RightPath.ToString() == "")
                    {
                        throw new Exception("Неверный путь");
                    }
                    oldpath = LeftPath.ToString();
                    newpath = RightPath.ToString() + name;
                }
                else
                {
                    if (LeftPath.ToString() == "")
                    {
                        throw new Exception("Неверный путь");
                    }
                    oldpath = RightPath.ToString();
                    newpath = LeftPath.ToString() + name;
                }
                if (element.isElement == IsElement.IsDirectory)
                {
                    if (!new DirectoryInfo(newpath).Exists)
                    {
                        DirectoryInfo newdirect = new DirectoryInfo(newpath);
                        newdirect.Create();

                        GetSubDir(new DirectoryInfo(oldpath + name), newpath, delete);
                        try
                        {
                            if (delete)
                            {
                                Directory.Delete(oldpath + name, true);
                            }
                        }
                        catch (IOException exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                    }
                }
                else if (element.isElement == IsElement.IsFile)
                {
                    if (delete)
                    {
                        try
                        {
                            File.Move(oldpath + name, newpath);
                        }
                        catch (IOException exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            File.Copy(oldpath + name, newpath, false);
                        }
                        catch (IOException exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                        catch (UnauthorizedAccessException exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                    }
                }
                UpdateFiles();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
Пример #4
0
        private async void Delete_Click(object sender, RoutedEventArgs e)
        {
            IsWorked = true;
            string path;

            if (LeftList.SelectedItems.Count > 0 && LeftPath.Length > 0)
            {
                await Task.Run(() =>
                {
                    Element[] elements;
                    GetNewCollection(out elements, true);
                    path = LeftPath.ToString();
                    foreach (Element element in elements)
                    {
                        Delete(element, path);
                    }
                });
            }
            else if (RightList.SelectedItems.Count > 0 && RightPath.Length > 0)
            {
                await Task.Run(() =>
                {
                    path = RightPath.ToString();
                    Element[] elements;
                    GetNewCollection(out elements, false);
                    foreach (Element element in elements)
                    {
                        Delete(element, path);
                    }
                });
            }
            else
            {
                if (LeftPath.Length == 0 && RightPath.Length == 0 && (LeftList.SelectedItems.Count > 0 || RightList.SelectedItems.Count > 0))
                {
                    MessageBox.Show("Диск не подлежит удалению");
                }
                else
                {
                    MessageBox.Show("Не выбран элемент для удаления");
                }
                IsWorked = false;
                return;
            }
            void Delete(Element elementfordel, String pathfordel)
            {
                if (elementfordel.isElement == IsElement.IsDirectory)
                {
                    try
                    {
                        Directory.Delete(pathfordel + elementfordel.Name, true);
                    }
                    catch (IOException exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                    catch (UnauthorizedAccessException exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
                else
                {
                    try
                    {
                        File.Delete(pathfordel + elementfordel.Name);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
                UpdateFiles();
            }

            LeftList.SelectedIndex  = -1;
            RightList.SelectedIndex = -1;
            IsWorked = false;
        }
        private void Rename(object sender, RoutedEventArgs e)
        {
            Element element;
            string  path;

            if (LeftList.SelectedItem != null && LeftList.SelectedItems.Count == 1)
            {
                element = (Element)LeftList.SelectedItem;
                path    = LeftPath.ToString();
            }
            else if (RightList.SelectedItem != null && RightList.SelectedItems.Count == 1)
            {
                element = (Element)RightList.SelectedItem;
                path    = RightPath.ToString();
            }
            else
            {
                MessageBox.Show("Для переименования должен быть выбран 1 элемент");
                return;
            }
            if (element.isElement == IsElement.IsDrive)
            {
                MessageBox.Show("Разделы диска или носители нельзя переименовывать");
            }
            if (element.isElement == IsElement.IsDirectory)
            {
                DirectoryInfo directory    = new DirectoryInfo(path + element.Name);
                RenameWindow  renameWindow = new RenameWindow();
                renameWindow.ShowDialog();
                if (IsExistValue)
                {
                    if (renameWindow.TextBoxForName.Text != "")
                    {
                        try
                        {
                            directory.MoveTo(path + "\\" + renameWindow.TextBoxForName.Text);
                        }
                        catch (IOException exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Значение не введено");
                    }
                    IsExistValue = false;
                }
            }
            if (element.isElement == IsElement.IsFile)
            {
                FileInfo     file         = new FileInfo(path + element.Name);
                RenameWindow renameWindow = new RenameWindow();
                renameWindow.TextBoxForName.Text = file.Extension;
                renameWindow.ShowDialog();
                if (IsExistValue)
                {
                    if (renameWindow.TextBoxForName.Text != file.Extension)
                    {
                        try
                        {
                            file.MoveTo(path + "\\" + renameWindow.TextBoxForName.Text);
                        }
                        catch (IOException exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Значение не введено");
                    }
                }
            }
            UpdateFiles();
        }
        //private void Encode(object sender, RoutedEventArgs e)
        //{
        //    Element element;
        //    string path;
        //    if (LeftList.SelectedItem != null && LeftList.SelectedItems.Count == 1)
        //    {
        //        element = (Element)LeftList.SelectedItem;
        //        path = LeftPath.ToString();
        //    }
        //    else if (RightList.SelectedItem != null && RightList.SelectedItems.Count == 1)
        //    {
        //        element = (Element)RightList.SelectedItem;
        //        path = RightPath.ToString();
        //    }
        //    else
        //    {
        //        MessageBox.Show("Закодировать можно только 1 текстовый документ");
        //        return;
        //    }
        //    if(element.isElement == IsElement.IsFile)
        //    {
        //        FileInfo file = new FileInfo(path + "//" + element.Name);
        //        if(file.Extension == ".txt" || file.Extension == ".docx" || file.Extension == ".doc")
        //        {
        //            try
        //            {
        //                using (StreamReader reader = new StreamReader(File.Open(path + "//" + element.Name, FileMode.Open),Encoding.Default))
        //                {
        //                    using (StreamWriter writer = new StreamWriter(File.Open(path + "//" + "EncodingFile.docx", FileMode.CreateNew), Encoding.Default))
        //                    {
        //                        string temp;
        //                        StringBuilder stringBuilder = new StringBuilder(100);
        //                        while ((temp = reader.ReadLine()) != null)
        //                        {

        //                            for (int count = 0; count < temp.Length; count++)
        //                            {
        //                                stringBuilder.Append((char)((char)(temp[count]) + 5));
        //                            }
        //                            writer.WriteLine(stringBuilder.ToString());
        //                            stringBuilder.Clear();
        //                        }
        //                    }
        //                }
        //            }
        //            catch (Exception exc)
        //            {
        //                MessageBox.Show(exc.Message);
        //            }
        //            UpdateFiles();
        //        }
        //        else
        //        {
        //            MessageBox.Show("Документе не имеет требуемое расширение txt, docx или doc");
        //        }
        //    }
        //    else
        //    {
        //        MessageBox.Show("Документе должен быть файлом с расширением txt, docx или doc");
        //    }
        //}
        //private void Decode(object sender, RoutedEventArgs e)
        //{
        //    Element element;
        //    string path;
        //    if (LeftList.SelectedItem != null && LeftList.SelectedItems.Count == 1)
        //    {
        //        element = (Element)LeftList.SelectedItem;
        //        path = LeftPath.ToString();
        //    }
        //    else if (RightList.SelectedItem != null && RightList.SelectedItems.Count == 1)
        //    {
        //        element = (Element)RightList.SelectedItem;
        //        path = RightPath.ToString();
        //    }
        //    else
        //    {
        //        MessageBox.Show("Раскодировать можно только 1 текстовый документ");
        //        return;
        //    }
        //    if (element.isElement == IsElement.IsFile)
        //    {
        //        FileInfo file = new FileInfo(path + "//" + element.Name);
        //        if (file.Extension == ".txt" || file.Extension == ".docx" || file.Extension == ".doc")
        //        {
        //            try
        //            {
        //                using (StreamReader reader = new StreamReader(File.Open(path + "//" + element.Name, FileMode.Open), Encoding.Default))
        //                {
        //                    using (StreamWriter writer = new StreamWriter(File.Open(path + "//" + "DecodingFile.docx", FileMode.CreateNew), Encoding.Default))
        //                    {
        //                        string temp;
        //                        StringBuilder stringBuilder = new StringBuilder(100);
        //                        while ((temp = reader.ReadLine()) != null)
        //                        {

        //                            for (int count = 0; count < temp.Length; count++)
        //                            {
        //                                stringBuilder.Append((char)((char)(temp[count]) - 5));
        //                            }
        //                            writer.WriteLine(stringBuilder.ToString());
        //                            stringBuilder.Clear();
        //                        }
        //                    }
        //                }
        //            }
        //            catch (Exception exc)
        //            {
        //                MessageBox.Show(exc.Message);
        //            }

        //            UpdateFiles();
        //        }
        //        else
        //        {
        //            MessageBox.Show("Документе не имеет требуемое расширение txt, docx или doc");
        //        }
        //    }
        //    else
        //    {
        //        MessageBox.Show("Документе должен быть файлом с расширением txt, docx или doc");
        //    }
        //}

        private void Create(object sender, RoutedEventArgs e)
        {
            string   path     = "";
            MenuItem menuItem = (MenuItem)sender;

            if (menuItem.Parent.GetValue(NameProperty).ToString() == "CreateMenuLeft")
            {
                path = LeftPath.ToString();
            }
            else if (menuItem.Parent.GetValue(NameProperty).ToString() == "CreateMenuRight")
            {
                path = RightPath.ToString();
            }

            if (path != "")
            {
                RenameWindow window = new RenameWindow();
                string       name   = ((MenuItem)sender).Header.ToString();
                window.TextBoxForName.Text = name;
                window.button.Content      = "Создать";
                window.ShowDialog();
                if (IsExistValue)
                {
                    if (name == "новый каталог")
                    {
                        DirectoryInfo directory = new DirectoryInfo(path + window.TextBoxForName.Text);
                        if (!directory.Exists)
                        {
                            try
                            {
                                directory.Create();
                            }
                            catch (System.UnauthorizedAccessException exc)
                            {
                                MessageBox.Show(exc.Message);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Каталог с таким именем уже существует");
                        }
                    }
                    else
                    {
                        FileInfo file = new FileInfo(path + window.TextBoxForName.Text);

                        if (!file.Exists)
                        {
                            try
                            {
                                file.Create();
                            }
                            catch (System.UnauthorizedAccessException exc)
                            {
                                MessageBox.Show(exc.Message);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Данный элемент уже существует");
                        }
                    }
                    IsExistValue = false;
                    UpdateFiles();
                }
            }
            else
            {
                MessageBox.Show("Елемент должен быть создан в разделе диска либо в запоминающем устройстве");
            }
        }