ProjectTreeNode AddExstingDirectory(DirectoryInfo source, DirectoryInfo target, ProjectTreeNode parentNode, bool recursive) { ProjectTreeNode node = null; string targetName = target.Name; foreach (ProjectTreeNode child in parentNode.Nodes) { if (child.Text.ToLower() == targetName.ToLower()) { node = child; break; } } if (node == null) { node = parentNode.AddFolder(targetName); } // Check if the target directory exists, if not, create it. if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); } // Copy each file into it’s new directory. foreach (FileInfo fi in source.GetFiles()) { string targetFile = Path.Combine(target.ToString(), fi.Name); node.AddFile(fi.Name); if (!System.IO.File.Exists(targetFile)) { fi.CopyTo(targetFile, false); } } if (recursive) { // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); AddExstingDirectory(diSourceSubDir, nextTargetSubDir, node, recursive); } } return(node); }
void ProjectTreeView_MouseClick_PopupMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { if (!IsProjectOpen() || e.ClickedItem.Tag == null) { return; } ProjectTreeNode node = ((ProjectTreeNode)e.ClickedItem.Tag); if (sender.GetType() == typeof(ContextMenuStrip)) { ((ContextMenuStrip)sender).Close(); } bool clickHandled = true; if (e.ClickedItem.Text == "Folder") { node.Expand(); ProjectTreeNode newNode = node.CreateNewFolder(); _ProjectTree.SelectedNode = newNode; newNode.BeginEdit(); } else if (e.ClickedItem.Text == "Add Existing Folder") { FolderBrowserDialog addExisting = new FolderBrowserDialog(); addExisting.RootFolder = Environment.SpecialFolder.Desktop; addExisting.SelectedPath = Path.GetDirectoryName(ProjectFileName); addExisting.ShowNewFolderButton = false; if (addExisting.ShowDialog() == DialogResult.OK) { DirectoryInfo source = new DirectoryInfo(addExisting.SelectedPath); DirectoryInfo target = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(ProjectFileName), Path.GetFileName(addExisting.SelectedPath))); /* * if (target.FullName.StartsWith(source.FullName) || source.FullName.StartsWith(target.FullName)) * { * MessageBox.Show("Cannot add the parent of the currently selected folder."); * return; * } */ ProjectTreeNode newNode = AddExstingDirectory(source, target, node, true); if (newNode != null) { _ProjectTree.SelectedNode = newNode; newNode.Expand(); } } } else if (e.ClickedItem.Text == "Code File") { if (node.Parent != null) { node.Parent.Expand(); } ProjectTreeNode newNode = node.CreateNewFile(global.CodeFileExtension); _ProjectTree.SelectedNode = newNode; newNode.BeginEdit(); } else if (e.ClickedItem.Text == "HTML File") { if (node.Parent != null) { node.Parent.Expand(); } ProjectTreeNode newNode = node.CreateNewFile(".html"); _ProjectTree.SelectedNode = newNode; newNode.BeginEdit(); } else if (e.ClickedItem.Text == "Set as startup object") { node.IsStartupObject = !node.IsStartupObject; } else if (e.ClickedItem.Text == "Add Existing File") { System.Windows.Forms.OpenFileDialog ofdAddFile = new System.Windows.Forms.OpenFileDialog(); ofdAddFile.Multiselect = true; ofdAddFile.ShowHelp = false; ofdAddFile.DefaultExt = global.CodeFileExtension; ofdAddFile.CheckFileExists = true; ofdAddFile.CheckPathExists = true; ofdAddFile.ValidateNames = true; ofdAddFile.Filter = "Simple Script|*" + global.CodeFileExtension + "|Text Files|*.txt|All Files|*.*"; if (ofdAddFile.ShowDialog() == DialogResult.OK) { foreach (String sourceFileName in ofdAddFile.FileNames) { try { ProjectTreeNode newNode = node.AddFile(sourceFileName); string targetFileName = newNode.LogicalPath(); if (sourceFileName.ToLower() != targetFileName.ToLower()) { System.IO.File.Copy(sourceFileName, targetFileName); } } catch (Exception ex) { MessageBox.Show("Cannot add the file [" + sourceFileName + "] due to the following error:\r\n\r\n" + ex.Message); } } } } else if (e.ClickedItem.Text == "Explore Folder") { try { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.UseShellExecute = true; process.StartInfo.FileName = "explorer"; process.StartInfo.Arguments = node.LogicalPath(); process.Start(); } catch (Exception ex) { MessageBox.Show("Cannot explore to the folder due to the following error:\r\n\r\n" + ex.Message); } } else { clickHandled = false; } if (!clickHandled) //These operations affected some tabs to be closed. { clickHandled = true; if (!CloseAffectedProjectTabs(node)) { return; } if (e.ClickedItem.Text == "Rename") { node.BeginEdit(); } else if (e.ClickedItem.Text == "Remove") { if (MessageBox.Show("Are you sure you want to remove the " + node.BasicNodeType.ToString() + " \"" + node.Text + "\" from the project?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != System.Windows.Forms.DialogResult.Yes) { return; } node.Remove(); } else if (e.ClickedItem.Text == "Delete") { if (MessageBox.Show("Are you sure you want to PERMANENTLY delete the " + node.BasicNodeType.ToString() + " \"" + node.Text + "\"?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != System.Windows.Forms.DialogResult.Yes) { return; } try { if (node.BasicNodeType == ProjectTreeNode.BasicNodeTypes.File) { System.IO.File.Delete(node.LogicalPath()); node.Remove(); } else if (node.BasicNodeType == ProjectTreeNode.BasicNodeTypes.Folder) { Directory.Delete(node.LogicalPath(), true); node.Remove(); } } catch (Exception ex) { MessageBox.Show("The object cannot be deleted due to the error below, try removing it instead.\r\n\r\n" + ex.Message); } } else if (e.ClickedItem.Text == "Delete File") { } else { clickHandled = false; } } SaveProjectFile(); }