Exemplo n.º 1
0
        private void DoDeleteItem(object sender, EventArgs e)
        {
            var      translator = new Translator("Browser");
            TreeNode node       = tree.SelectedNode;

            DialogResult result = MessageBox.Show(
                String.Format(translator.GetString("ConfirmDeleteMessage"), node.Text),
                translator.GetString("ConfirmDeleteTitle"),
                MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);

            if (result == DialogResult.Yes)
            {
                string path = (string)node.Tag;
                if (File.GetAttributes(path).HasFlag(FileAttributes.Directory))
                {
                    if (Directory.Exists(path))
                    {
                        try
                        {
                            Directory.Delete(path, true);
                            node.Remove();
                        }
                        catch (Exception exc)
                        {
                            ExceptionDialog.ShowException(exc);
                        }
                    }
                }
                else
                {
                    if (File.Exists(path))
                    {
                        try
                        {
                            File.Delete(path);
                            node.Remove();
                        }
                        catch (Exception exc)
                        {
                            ExceptionDialog.ShowException(exc);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void tree_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label == null)
            {
                e.CancelEdit = true;
                return;
            }

            string label = e.Label.Trim();

            if (String.IsNullOrEmpty(label))
            {
                e.CancelEdit = true;
                return;
            }

            TreeNode node = e.Node;
            string   path = node.Tag as String;

            if (File.GetAttributes(path).HasFlag(FileAttributes.Directory))
            {
                if (!Path.GetFileNameWithoutExtension(path).Equals(
                        label, StringComparison.InvariantCultureIgnoreCase))
                {
                    string newpath = Path.Combine(Path.GetDirectoryName(path), label);

                    try
                    {
                        Directory.Move(path, newpath);
                        node.Tag = newpath;

                        RefreshFolder(node);
                        e.CancelEdit = true;
                    }
                    catch (Exception exc)
                    {
                        ExceptionDialog.ShowException(exc);
                        e.CancelEdit = true;
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(label) &&
                    !Path.GetFileNameWithoutExtension(path).Equals(
                        label, StringComparison.InvariantCultureIgnoreCase))
                {
                    string newpath = Path.Combine(
                        Path.GetDirectoryName(path), label + Path.GetExtension(path));

                    try
                    {
                        File.Move(path, newpath);
                        node.Tag = newpath;
                    }
                    catch (Exception exc)
                    {
                        ExceptionDialog.ShowException(exc);
                        e.CancelEdit = true;
                    }
                }
            }
        }