private void Excute(object sender, EventArgs e) { _myOutPane.Activate(); _lstExcludePath = _dte.GetExcludePathList(); _count = 0; _selectedMenu = (PkgCmdIdList)((MenuCommand)sender).CommandID.ID; var table = new RunningDocumentTable(this); _lstAlreadyOpenFiles = table.Select(p => p.Moniker).ToList(); var selectedItem = _dte.SelectedItems.Item(1); WriteLog($"{Environment.NewLine}===================================================================================="); WriteLog($"Start: {DateTime.Now}"); var sp = new Stopwatch(); sp.Start(); //以下判断只会进一次 if (selectedItem.Project == null && selectedItem.ProjectItem == null) //从解决方案一级进来的 { ProcessSolution(); } else if (selectedItem.Project != null) //从项目一级进来的 { ProcessProject(selectedItem.Project); } else if (selectedItem.ProjectItem != null) //从文件夹一级或者文件一级进来的 { if (selectedItem.ProjectItem.ProjectItems.Count > 0) //此项下页还有文件,如:文件夹,T4模板 { ProcessProjectItem(selectedItem.ProjectItem); ProcessProjectItems(selectedItem.ProjectItem.ProjectItems); } else //此项下什么都没有了,直接处理 { ProcessProjectItem(selectedItem.ProjectItem); } } sp.Stop(); WriteLog($"Finish: {DateTime.Now} Files: {_count - 2} Elapsed: {sp.ElapsedMilliseconds / 1000}s"); _dte.ExecuteCommand("View.Output"); _myOutPane.Activate(); }
/// <summary> /// Removes unused usings from project classes. /// </summary> /// <param name="serviceProvider">Service provider.</param> public override void RemoveUnusedUsings(System.IServiceProvider serviceProvider) { base.RemoveUnusedUsings(serviceProvider); RunningDocumentTable docTable = new RunningDocumentTable(serviceProvider); var alreadyOpenFiles = docTable.Select(info => info.Moniker).ToList(); string fileName; foreach (ProjectItem projectItem in new ProjectItemIterator(Project.ProjectItems).Where(item => item.FileCodeModel != null)) { fileName = projectItem.get_FileNames(0); Window window = Project.DTE.OpenFile(EnvDTE.Constants.vsViewKindTextView, fileName); window.Activate(); try { Project.DTE.ExecuteCommand("Edit.RemoveAndSort", string.Empty); } catch (COMException e) { //Do nothing, go to the next item if (LogManager.ActivityLog != null) { LogManager.ActivityLog.Error(null, e); } } if (alreadyOpenFiles.SingleOrDefault(file => file.Equals(fileName, StringComparison.OrdinalIgnoreCase)) != null) { Project.DTE.ActiveDocument.Save(fileName); } else { window.Close(vsSaveChanges.vsSaveChangesYes); } } }