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.OrderBy(f => !File.GetAttributes(f).HasFlag(FileAttributes.Directory)).ThenBy(f => Path.GetFileName(f))) { if (File.GetAttributes(file).HasFlag(FileAttributes.Directory)) { var di = new DirectoryInfo(file); var folderNode = new TreeNode(di.Name) { ImageIndex = 15, SelectedImageIndex = 15, Tag = di.FullName, Name = di.Name }; currentNode.Nodes.Add(folderNode); CreateFolderStructure(folderNode, new DirectoryInfo(file), errorList, null); continue; } var fi = new FileInfo(file); string nodeObjectName = GetName(currentNode); // Test valid characters if (WebResource.IsNameValid(fi.Name)) { // Create CRM web resource var newWebResource = WebResource.LoadWebResourceFromDisk(file, string.Format("{0}/{1}", nodeObjectName, fi.Name)); // 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)) + 14 }; node.SelectedImageIndex = node.ImageIndex; node.Tag = newWebResource; newWebResource.Node = node; newWebResource.ReinitStatus(); //newWebResource.State = WebresourceState.None; newWebResource.WebresourceStateChanged += Wr_WebresourceStateChanged; newWebResource.Node = node; currentNode.Nodes.Add(node); currentNode.Expand(); } else { errorList.Add(file); } } 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.IsNameValid(fi.Name)) { var wr = WebResource.LoadWebResourceFromDisk(fileName, string.Format("{0}/{1}", name, fi.Name), string.Format("{0}/{1}", name, fi.Name)); var node = new TreeNode(fi.Name) { ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1)) + 14, Tag = wr }; node.SelectedImageIndex = node.ImageIndex; wr.Node = node; wr.ReinitStatus(); //wr.State = WebresourceState.None; wr.WebresourceStateChanged += Wr_WebresourceStateChanged; WebResources.Add(wr); selectedNode.Nodes.Add(node); selectedNode.Expand(); } else { errorList.Add(fileName); } 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(); WebResources.Clear(); Invoke(new Action(() => { pnlWaitingPublish.Visible = false; })); 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.IsNameValid(diChild.Name)) { // Create a root treenode var rootFolderNode = new TreeNode(diChild.Name) { ImageIndex = 14, Tag = diChild.FullName }; tv.Nodes.Add(rootFolderNode); // Add child folders CreateFolderStructure(rootFolderNode, diChild, invalidFilenames, extensionsToLoad); } else { invalidFilenames.Add(diChild.FullName); } } // 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.IsNameValid(fiChild.Name) && WebResource.IsValidExtension(fiChild.Extension)) { // If the file is of type we want to load, the node is created if (extensionsToLoad.Contains(fiChild.Extension)) { CreateWebResourceNode(fiChild, tv); } } else if (!WebResource.IsNameValid(fiChild.Name) || !WebResource.SkipErrorForInvalidExtension(fiChild.Extension)) { invalidFilenames.Add(fiChild.FullName); } } if (Options.Instance.ExpandAllOnLoadingResources) { tv.ExpandAll(); } tv.TreeViewNodeSorter = new NodeSorter(); tv.Sort(); return(invalidFilenames); }