private void UpdateFolderStructure(TreeNode parentFolderNode, DirectoryInfo di, List <string> invalidFilenames, List <string> extensionsToLoad) { var subNodes = parentFolderNode.Nodes; var subFolders = di.GetDirectories(); foreach (var subFolder in subFolders) { if (!subNodes.ContainsKey(subFolder.Name) || subNodes[subFolder.Name].ImageIndex != 1) { var folderNode = new TreeNode(subFolder.Name) { ImageIndex = 1, SelectedImageIndex = 1, Tag = subFolder.FullName, Name = subFolder.Name }; parentFolderNode.Nodes.Add(folderNode); UpdateFolderStructure(folderNode, subFolder, invalidFilenames, extensionsToLoad); } else { UpdateFolderStructure(subNodes[subFolder.Name], subFolder, invalidFilenames, extensionsToLoad); } } foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { if (WebResource.IsNameInvalid(fiChild.Name) || !WebResource.ValidExtensions.Contains(fiChild.Extension.Remove(0, 1).ToLower())) { invalidFilenames.Add(fiChild.FullName); continue; } if (extensionsToLoad == null || extensionsToLoad.Contains(fiChild.Extension)) { if (!subNodes.ContainsKey(fiChild.Name) || subNodes[fiChild.Name].ImageIndex <= 1) { CreateWebResourceNode(fiChild, parentFolderNode); } else { var wr = (WebResource)subNodes[fiChild.Name].Tag; wr.Entity["content"] = Convert.ToBase64String(File.ReadAllBytes(wr.FilePath)); } } } }
/// <summary> /// Create a TreeView folder structure depending on the DirectoryInfo /// element in parameters /// </summary> /// <param name="parentFolderNode">Current TreeNode</param> /// <param name="di">Current physical directory info</param> /// <param name="invalidFilenames"></param> /// <param name="extensionsToLoad"></param> private void CreateFolderStructure(TreeNode parentFolderNode, DirectoryInfo di, List <string> invalidFilenames, List <string> extensionsToLoad) { foreach (DirectoryInfo diChild in di.GetDirectories()) { if (WebResource.IsNameInvalid(diChild.Name)) { invalidFilenames.Add(diChild.FullName); continue; } // If the current physical directory has sub directories or // javascript file, a new TreeNode has to be created if (diChild.GetDirectories().Length > 0 || diChild.GetFiles("*.*").Length > 0) { var folderNode = new TreeNode(diChild.Name) { ImageIndex = 1, SelectedImageIndex = 1, Tag = diChild.FullName, Name = diChild.Name }; parentFolderNode.Nodes.Add(folderNode); CreateFolderStructure(folderNode, diChild, invalidFilenames, extensionsToLoad); } } foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { if (WebResource.IsNameInvalid(fiChild.Name) || !WebResource.ValidExtensions.Contains(fiChild.Extension.Remove(0, 1).ToLower())) { invalidFilenames.Add(fiChild.FullName); continue; } if (extensionsToLoad == null || extensionsToLoad.Contains(fiChild.Extension)) { // Create a TreeNode for each javascript file CreateWebResourceNode(fiChild, parentFolderNode); } } }
private void tv_DragDrop(object sender, DragEventArgs e) { var errorList = new List <string>(); // Retrieve the current selected node var treeview = (TreeView)sender; var location = tv.PointToScreen(Point.Empty); var currentNode = treeview.GetNodeAt(e.X - location.X, e.Y - location.Y); var files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (var file in files) { var fi = new FileInfo(file); string nodeObjectName = GetName(currentNode); // Test valid characters if (WebResource.IsNameInvalid(fi.Name)) { errorList.Add(file); } else { // Create CRM web resource var webResource = new Entity("webresource") { ["content"] = Convert.ToBase64String(File.ReadAllBytes(file)), ["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1))), ["name"] = string.Format("{0}/{1}", nodeObjectName, fi.Name), ["displayname"] = string.Format("{0}/{1}", nodeObjectName, fi.Name) }; var newWebResource = new WebResource(webResource, fi.FullName); // Create file if the current node has a filepath in its tag // this means, wen resources come from disk if (currentNode.Tag != null && currentNode.Tag is string && Directory.Exists(currentNode.Tag.ToString())) { var resultingFileName = Path.Combine(currentNode.Tag.ToString(), fi.Name); if (resultingFileName.ToLower() != fi.FullName.ToLower()) { if (DialogResult.Yes == MessageBox.Show( "Would you like to also copy this file to folder '" + currentNode.Tag + "'?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { File.WriteAllBytes(resultingFileName, File.ReadAllBytes(file)); } } } var node = new TreeNode(fi.Name) { ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1)) }; node.SelectedImageIndex = node.ImageIndex; node.Tag = newWebResource; newWebResource.Node = node; currentNode.Nodes.Add(node); currentNode.Expand(); } } if (errorList.Any()) { MessageBox.Show(ParentForm, Resources.WebresourceTreeView_InvalidFileNameWarningMessage + string.Join("\r\n", errorList), Resources.MessageBox_WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// Add existing files from disk as new web resources /// </summary> public void AddExistingWebResource() { TreeNode selectedNode = tv.SelectedNode; TreeNode tempNode = selectedNode; string name = tempNode.Text; while (tempNode.Parent != null) { name = string.Format("{0}/{1}", tempNode.Parent.Text, name); tempNode = tempNode.Parent; } var ofd = new OpenFileDialog { Multiselect = true, Title = "Select file(s) for web resource(s)" }; if (ofd.ShowDialog(ParentForm) == DialogResult.OK) { var errorList = new List <string>(); foreach (string fileName in ofd.FileNames) { var fi = new FileInfo(fileName); //Test valid characters if (WebResource.IsNameInvalid(fi.Name)) { errorList.Add(fileName); } else { var webResource = new Entity("webresource"); webResource["content"] = Convert.ToBase64String(File.ReadAllBytes(fileName)); webResource["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1))); webResource["name"] = string.Format("{0}/{1}", name, fi.Name); webResource["displayname"] = string.Format("{0}/{1}", name, fi.Name); var wr = new WebResource(webResource, fileName); var node = new TreeNode(fi.Name) { ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1)) }; node.SelectedImageIndex = node.ImageIndex; node.Tag = wr; selectedNode.Nodes.Add(node); selectedNode.Expand(); } if (errorList.Count > 0) { MessageBox.Show("Some file have not been added since their name does not match naming policy\r\n" + string.Join("\r\n", errorList)); } } } tv.TreeViewNodeSorter = new NodeSorter(); tv.Sort(); }
/// <summary> /// Retrieve from disk and store web resources in the current control /// </summary> /// <param name="folderPath">Path of the folder from where to load the resource</param> /// <param name="extensionsToLoad">Extensions valid for files to load</param> public List <string> LoadWebResourcesFromDisk(string folderPath, List <string> extensionsToLoad) { tv.Nodes.Clear(); var invalidFilenames = new List <string>(); var di = new DirectoryInfo(folderPath); // Only folder ending with "_" character are processed. They represent // the prefix of customizations foreach (DirectoryInfo diChild in di.GetDirectories("*_", SearchOption.TopDirectoryOnly)) { if (WebResource.IsNameInvalid(diChild.Name)) { invalidFilenames.Add(diChild.FullName); continue; } // Create a root treenode var rootFolderNode = new TreeNode(diChild.Name) { ImageIndex = 0, Tag = diChild.FullName }; tv.Nodes.Add(rootFolderNode); // Add child folders CreateFolderStructure(rootFolderNode, diChild, invalidFilenames, extensionsToLoad); } // For each files that wouldn't use virtual folders structure, all // files are processed foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { // We cannot process files without extensions since we // won't be able to identify the type of file if (fiChild.Extension.Length == 0) { invalidFilenames.Add(fiChild.FullName); continue; } // Do not process files with invalid names or extensions not related // to web resources if (WebResource.IsNameInvalid(fiChild.Name) || !WebResource.ValidExtensions.Contains(fiChild.Extension.Remove(0, 1).ToLower())) { invalidFilenames.Add(fiChild.FullName); continue; } // If the file is of type we want to load, the node is created if (extensionsToLoad.Contains(fiChild.Extension)) { CreateWebResourceNode(fiChild, tv); } } tv.ExpandAll(); tv.TreeViewNodeSorter = new NodeSorter(); tv.Sort(); return(invalidFilenames); }