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); } }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void Execute(object sender, EventArgs e) { //var button = (MenuCommand)sender; // 右键点击后选中的文件(一般是一个,但也可以多个) var items = ProjectHelpers.GetSelectedItemPaths(); if (items.Count() == 0) { ErrBox.Info(this.package, "未选中文件!"); return; } // 当前活动项目路径 Project activeProj = ProjectHelpers.GetActiveProject(); if (activeProj == null) { ErrBox.Info(this.package, "未选中WEB项目"); return; } // 建立发布配置对象 EnvVar.ProjectDir = activeProj.GetRootFolder(); string res = PublishHelpers.CreatePublishCfg(); if (res != null) { ErrBox.Info(this.package, res); return; } // 取得要发布的文件路径 List <string> srcfiles = new List <string>(); foreach (string path in items) { // 可能选中的是目录 if (File.Exists(path)) { srcfiles.Add(path); } } if (srcfiles.Count == 0) { ErrBox.Info(this.package, "至少选择一个文件!"); return; } // 发布处理 string resinfo = PublishHelpers.PublishFiles(srcfiles); if (resinfo != null) { ErrBox.Info(this.package, resinfo); } }
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); } }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void Execute(object sender, EventArgs e) { //var button = (MenuCommand)sender; // 右键点击后选中的文件夹(一般是一个,但也可以多个) var selectedDirs = ProjectHelpers.GetSelectedItemPaths(); if (selectedDirs.Count() == 0) { ErrBox.Info(this.package, "未选中文件夹!"); return; } // 当前活动项目路径 Project activeProj = ProjectHelpers.GetActiveProject(); if (activeProj == null) { ErrBox.Info(this.package, "未选中WEB项目"); return; } // 建立发布配置对象 EnvVar.ProjectDir = activeProj.GetRootFolder(); string res = PublishHelpers.CreatePublishCfg(); if (res != null) { ErrBox.Info(this.package, res); return; } // 取得要发布的文件路径 List<string> srcfiles = new List<string>(); foreach (string dirPath in selectedDirs) { // 选中的可能是文件和目录,如果是目录才取出其中文件和子目录文件 if (Directory.Exists(dirPath)) srcfiles.AddRange(Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories)); } if (srcfiles.Count == 0) { ErrBox.Info(this.package, "至少选择一个文件夹!"); return; } // 发布处理 string resinfo = PublishHelpers.PublishFiles(srcfiles); if (resinfo != null) { ErrBox.Info(this.package, resinfo); } }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void Execute(object sender, EventArgs e) { // 发布当前处于活动状态的1个文件 如果没有活动文件,不动作 var activedoc = ProjectHelpers.GetActiveDoc(); if (activedoc == null) { ErrBox.Info(this.package, "未找到激活的文件"); return; } // 当前活动项目路径 Project activeProj = ProjectHelpers.GetActiveProject(); if (activeProj == null) { ErrBox.Info(this.package, "未选中WEB项目"); return; } // 建立发布配置对象 EnvVar.ProjectDir = activeProj.GetRootFolder(); string res = PublishHelpers.CreatePublishCfg(); if (res != null) { ErrBox.Info(this.package, res); return; } // 取得要发布的文件路径 List <string> srcfiles = new List <string> { activedoc.FullName }; // 发布处理 string resinfo = PublishHelpers.PublishFiles(srcfiles); if (resinfo != null) { ErrBox.Info(this.package, resinfo); } }
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); } }