/// <summary> /// Adds the project reference. /// </summary> /// <param name="projectId">The project id.</param> public void AddProjectReference(Guid projectId) { if (ProjectGuid == projectId) { return; } ProjectNode referencedProject = new ProjectNode(Solution, projectId); if (VsProject != null) { if (VsProject.Object is VSProject project) { References references = project.References; if (references != null && referencedProject.ExtObject is EnvDTE.Project) { references.AddProject(referencedProject.ExtObject as EnvDTE.Project); } } else { AssemblyReferences references = (VsProject.Object as VSWebSite)?.References; if (references != null && referencedProject.ExtObject is EnvDTE.Project) { try { references.AddFromProject(referencedProject.ExtObject as EnvDTE.Project); } catch (COMException) { //Web projects throws exceptions if the reference already exists } } } } }
private void rMenuModifySelection_Click(object sender, EventArgs e) { if (this.dataGridView1.SelectedRows == null || this.dataGridView1.SelectedRows.Count < 1) { return; } References dllRefs = this.dataGridView1.Tag as References; if (dllRefs == null) { return; } string dllRootDir = this.txtLibRootDir.Text.Trim(); string dotNetBarDir = this.txtDotNetBarDir.Text.Trim(); string infragisticsDir = this.txtInfragisticsDir.Text.Trim(); if (string.IsNullOrEmpty(dllRootDir) || Directory.Exists(dllRootDir) == false) { MsgBox.ShowTip("请先输入Lib根目录"); return; } bool proItemFirst = this.chkProItemFirst.Checked; string solPlatform = this.combSolutionPlatform.SelectedItem.ToString(); this.Cursor = Cursors.WaitCursor; //读取lib和第三方类库 _dirModel = FileDirUtility.ReadAllDirAndFiles(dllRootDir); if (string.IsNullOrEmpty(dotNetBarDir) == false && Directory.Exists(dotNetBarDir)) { _dotNetBarDirModel = FileDirUtility.ReadAllDirAndFiles(dotNetBarDir); } if (string.IsNullOrEmpty(infragisticsDir) == false && Directory.Exists(infragisticsDir)) { _infragisticsDirModel = FileDirUtility.ReadAllDirAndFiles(infragisticsDir); } for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++) { DataGridViewRow selRow = this.dataGridView1.SelectedRows[i]; Reference dllRef = selRow.Tag as Reference; if (dllRef == null) { continue; } //优先引用项目,再引用lib下的dll Project bestSimilarProj = null; if (proItemFirst) { string dllName = dllRef.Name; bestSimilarProj = this.FindProItem(dllName, solPlatform); string bestSimilarDll = this.FindProItemOutPath(dllName, solPlatform); if (bestSimilarProj != null) { string dllFilePath = this.GetOutPutDllPath(bestSimilarProj, solPlatform); if (dllFilePath.Equals(dllRef.Path, StringComparison.CurrentCultureIgnoreCase)) { continue; } selRow.Tag = null; dllRef.Remove(); dllRef = dllRefs.AddProject(bestSimilarProj); selRow.Tag = dllRef; selRow.Cells[0].Value = Path.GetFileNameWithoutExtension(bestSimilarDll); selRow.Cells[1].Value = bestSimilarDll; } } if (bestSimilarProj == null) { List <string> similarDlls = new List <string>(); if (_dotNetBarDirModel != null && dllRef.Name.ToUpper().Contains("DevComponents".ToUpper())) { this.FindSimilarDlls(_dotNetBarDirModel, dllRef.Name, ref similarDlls); } else if (_infragisticsDirModel != null && dllRef.Name.ToUpper().Contains("Infragistics".ToUpper())) { this.FindSimilarDlls(_infragisticsDirModel, dllRef.Name, ref similarDlls); } if (similarDlls.Count == 0) { this.FindSimilarDlls(_dirModel, dllRef.Name, ref similarDlls); } if (similarDlls.Count < 1) { continue; } string bestSimilarDll = similarDlls[0]; if (similarDlls.Count > 1) { double maxSim = double.MinValue; int maxSimIndex = 0; for (int j = 0; j < similarDlls.Count; j++) { double sim = StrSimCalculator.CalculateSim(similarDlls[j], dllRef.Path); if (maxSim < sim) { maxSim = sim; maxSimIndex = j; } } bestSimilarDll = similarDlls[maxSimIndex]; } if (bestSimilarDll.Equals(dllRef.Path, StringComparison.CurrentCultureIgnoreCase)) { continue; } selRow.Tag = null; dllRef.Remove(); try { dllRef = dllRefs.Add(bestSimilarDll); } catch { } selRow.Tag = dllRef; selRow.Cells[0].Value = Path.GetFileNameWithoutExtension(bestSimilarDll); selRow.Cells[1].Value = bestSimilarDll; } } this.Cursor = Cursors.Default; }
private void rMenuModifyAll_Click(object sender, EventArgs e) { if (this.combProjects.Tag == null) { return; } string dllRootDir = this.txtLibRootDir.Text.Trim(); string dotNetBarDir = this.txtDotNetBarDir.Text.Trim(); string infragisticsDir = this.txtInfragisticsDir.Text.Trim(); if (string.IsNullOrEmpty(dllRootDir) || Directory.Exists(dllRootDir) == false) { MsgBox.ShowTip("请先输入Lib根目录"); return; } bool proItemFirst = this.chkProItemFirst.Checked; string solPlatform = this.combSolutionPlatform.SelectedItem.ToString(); this.Cursor = Cursors.WaitCursor; //读取lib和第三方类库 _dirModel = FileDirUtility.ReadAllDirAndFiles(dllRootDir); if (string.IsNullOrEmpty(dotNetBarDir) == false && Directory.Exists(dotNetBarDir)) { _dotNetBarDirModel = FileDirUtility.ReadAllDirAndFiles(dotNetBarDir); } if (string.IsNullOrEmpty(infragisticsDir) == false && Directory.Exists(infragisticsDir)) { _infragisticsDirModel = FileDirUtility.ReadAllDirAndFiles(infragisticsDir); } List <Project> prjList = this.combProjects.Tag as List <Project>; for (int p = 0; p < prjList.Count; p++) { VSProject vsProj = prjList[p].Object as VSProject; References dllRefs = vsProj.References; if (dllRefs == null) { continue; } foreach (Reference dllRef in dllRefs) { if (this.IsNeedChangePath(dllRef.Path) == false) { continue; } //优先引用项目,再引用lib下的dll Project bestSimilarProj = null; if (proItemFirst) { string dllName = dllRef.Name; bestSimilarProj = this.FindProItem(dllName, solPlatform); if (bestSimilarProj != null) { string dllFilePath = this.GetOutPutDllPath(bestSimilarProj, solPlatform); if (dllFilePath.Equals(dllRef.Path, StringComparison.CurrentCultureIgnoreCase)) { continue; } dllRef.Remove(); dllRefs.AddProject(bestSimilarProj); } } if (bestSimilarProj == null) { List <string> similarDlls = new List <string>(); if (_dotNetBarDirModel != null && dllRef.Name.ToUpper().Contains("DevComponents".ToUpper())) { this.FindSimilarDlls(_dotNetBarDirModel, dllRef.Name, ref similarDlls); } else if (_infragisticsDirModel != null && dllRef.Name.ToUpper().Contains("Infragistics".ToUpper())) { this.FindSimilarDlls(_infragisticsDirModel, dllRef.Name, ref similarDlls); } if (similarDlls.Count == 0) { this.FindSimilarDlls(_dirModel, dllRef.Name, ref similarDlls); } if (similarDlls.Count < 1) { continue; } string bestSimilarDll = similarDlls[0]; if (similarDlls.Count > 1) { double maxSim = double.MinValue; int maxSimIndex = 0; for (int j = 0; j < similarDlls.Count; j++) { double sim = StrSimCalculator.CalculateSim(similarDlls[j], dllRef.Path); if (maxSim < sim) { maxSim = sim; maxSimIndex = j; } } bestSimilarDll = similarDlls[maxSimIndex]; } if (bestSimilarDll.Equals(dllRef.Path, StringComparison.CurrentCultureIgnoreCase)) { continue; } dllRef.Remove(); try { dllRefs.Add(bestSimilarDll); } catch { } } } } this.combProjects_SelectedIndexChanged(null, null); this.Cursor = Cursors.Default; }