public static void AddNestedFile(string parentFile, string newFile, string itemType = null) { ProjectItem item = _dte.Solution.FindProjectItem(parentFile); try { if (item == null || item.ContainingProject == null || item.ContainingProject.IsKind(ProjectTypes.ASPNET_5)) { return; } if (item.ProjectItems == null || item.ContainingProject.IsKind(ProjectTypes.UNIVERSAL_APP)) { item.ContainingProject.AddFileToProject(newFile); } else if (_dte.Solution.FindProjectItem(newFile) == null) { item.ProjectItems.AddFromFile(newFile); } ProjectItem newItem = _dte.Solution.FindProjectItem(newFile); newItem.SetItemType(itemType); } catch (Exception ex) { //Logger.Log(ex); ErrBox.Error(ex); } }
public static Project GetActiveProject() { try { Window2 window = _dte.ActiveWindow as Window2; Document doc = _dte.ActiveDocument; if (window != null && window.Type == vsWindowType.vsWindowTypeDocument) { // if a document is active, use the document's containing directory if (doc != null && !string.IsNullOrEmpty(doc.FullName)) { ProjectItem docItem = _dte.Solution.FindProjectItem(doc.FullName); if (docItem != null && docItem.ContainingProject != null) { return(docItem.ContainingProject); } } } Array activeSolutionProjects = _dte.ActiveSolutionProjects as Array; if (activeSolutionProjects != null && activeSolutionProjects.Length > 0) { return(activeSolutionProjects.GetValue(0) as Project); } if (doc != null && !string.IsNullOrEmpty(doc.FullName)) { var item = _dte.Solution?.FindProjectItem(doc.FullName); if (item != null) { return(item.ContainingProject); } } } catch (Exception ex) { //Logger.Log("Error getting the active project" + ex); ErrBox.Error(ex); } return(null); }
public static void DeleteFileFromProject(string file) { ProjectItem item = _dte.Solution.FindProjectItem(file); if (item == null) { return; } try { item.Delete(); } catch (Exception ex) { //Logger.Log(ex); ErrBox.Error(ex); } }
public static void SetItemType(this ProjectItem item, string itemType) { try { if (item == null || item.ContainingProject == null) { return; } if (string.IsNullOrEmpty(itemType) || item.ContainingProject.IsKind(ProjectTypes.WEBSITE_PROJECT) || item.ContainingProject.IsKind(ProjectTypes.UNIVERSAL_APP)) { return; } item.Properties.Item("ItemType").Value = itemType; } catch (Exception ex) { ErrBox.Error(ex); //Logger.Log(ex); } }