예제 #1
0
        /*Click event for the new folder button*/
        private void btnNewDir_Click(object sender, EventArgs e)
        {
            TreeNode node = this.treeView1.SelectedNode;//Get the selected node

            if (node == null)
            {
                return;                                   //Failsafe
            }
            Rename ren = new Rename();                    //We reuse the rename form for this action too, since it has everything we need

            ren.Text            = "Create new folder..."; //We just change some of the text to suit it's new purpose
            ren.label1.Text     = "Enter name for the new folder...";
            ren.label1.Location = new Point(5, 11);
            if (node.Nodes.Count == 0 && node.Tag == null)                //check if file is selected
            {
                node = node.Parent;                                       //Get the parent of the file node, assuming that they want this new folder in the same directory as the selected file
            }
            if (ren.ShowDialog(this) == DialogResult.OK)                  //Once they've settled on a name for their new folder
            {
                string dirName = ren.txtName.Text;                        //Get the name they want
                if (dirName == "Locker")                                  //If they've tried to name the folder "Locker"
                {
                    MessageBox.Show("Folder name cannot be \"Locker\"!"); //This could cause some confusion
                }
                else if (dirName != "")                                   //Make sure the name isn't empty
                {
                    string parnetPath = node.Tag.ToString();              //Get the path to the parent directory
                    try
                    {
                        Directory.CreateDirectory(parnetPath + "\\" + dirName); //Create the new directory
                    }
                    catch (Exception ex)                                        //If the heavens dost command we shall not
                    {
                        MessageBox.Show("Couldn't create folder.\nReason: " + ex.Message);
                    }
                }
                else//They gave us a blank name.
                {
                    MessageBox.Show("Folder name cannot be blank!");
                }
            }
            refreshTree();//Recreate our directory tree to reflect the changes
        }
예제 #2
0
        /*Click event for the rename button*/
        private void btnRename_Click(object sender, EventArgs e)
        {
            TreeNode node = this.treeView1.SelectedNode;//Get selected node

            if (node == null)
            {
                return;                                          //Failsafe
            }
            if (node != this.treeView1.Nodes[0])                 //Make sure its not the .locker node
            {
                if (node.Nodes.Count == 0 && node.Tag == null)   //Check if its a file
                {
                    Rename ren = new Rename();                   //Instance of Rename form
                    if (ren.ShowDialog(this) == DialogResult.OK) //Action once rename form closes
                    {
                        string newName = ren.txtName.Text;       //Get the name they typed
                        if (newName == "Locker")                 //Check if they tried to call it "Locker"
                        {
                            MessageBox.Show("Name cannot be \"Locker\"!");
                        }
                        else if (newName != "")                                                                         //Make sure name isn't empty
                        {
                            if (newName.LastIndexOf(".") == -1)                                                         //If there's no dot (they didn't give a file extension)
                            {
                                string ext     = node.Name.Substring(node.Name.LastIndexOf("."));                       //Figure out the extension on the old name, we'll keep it
                                string oldName = node.Name;                                                             //Store the old name
                                node.Text = newName + ext;                                                              //Create the new name from their provided name plus the old extension
                                node.Name = node.Name.Substring(0, node.Name.LastIndexOf("\\")) + "\\" + newName + ext; //Create the new path to the file
                                try
                                {
                                    File.Move(oldName, node.Name); //"Move" the file from it's old location to the new. This will rename it
                                }
                                catch (Exception ex)               //If something goes wrong...
                                {
                                    MessageBox.Show("Couldn't rename file.\nReason: " + ex.Message);
                                }
                            }
                            else//Rename with a new file extension
                            {
                                string oldName = node.Name;//Store the old name
                                node.Text = newName;                                                              //Give the node the new name
                                node.Name = node.Name.Substring(0, node.Name.LastIndexOf("\\")) + "\\" + newName; //Create the new path
                                try
                                {
                                    File.Move(oldName, node.Name); //"Move" it in order to rename it
                                }
                                catch (Exception ex)               //If they meet with a terrible fate...
                                {
                                    MessageBox.Show("Couldn't rename file.\nReason: " + ex.Message);
                                }
                            }
                        }
                        else//The provided name was blank
                        {
                            MessageBox.Show("File name cannot be blank!");
                        }
                    }
                }
                else if (node.Tag != null)                                                                                        //Check if it's a folder
                {
                    Rename ren = new Rename();                                                                                    //New instance of the rename form
                    if (ren.ShowDialog(this) == DialogResult.OK)                                                                  //Action once rename form closes
                    {
                        string newName = ren.txtName.Text;                                                                        //Get the name they gave us
                        if (newName != "")                                                                                        //If it's not blank
                        {
                            string oldPath = node.Tag.ToString();                                                                 //Store the old path
                            node.Text = newName;                                                                                  //Give the node the new name
                            node.Tag  = node.Tag.ToString().Substring(0, node.Tag.ToString().LastIndexOf("\\")) + "\\" + newName; //Change the node's path to reflect the new name
                            try
                            {
                                Directory.Move(oldPath, node.Tag.ToString()); //Actually rename the directory
                            }
                            catch (Exception ex)                              //If Thanos snaps his fingers
                            {
                                MessageBox.Show("Couldn't rename folder.\nReason: " + ex.Message);
                            }
                        }
                        else//They gave us a blank name, didn't they?
                        {
                            MessageBox.Show("Folder name cannot be blank!");
                        }
                    }
                }
            }
            else//They tried to rename the .locker node. We can't let them do that!
            {
                MessageBox.Show("You can't do that!");
            }
        }