public static void RemoveProjectReference(this SolutionFile solutionFile, string solutionFilePath, string projectFilePath) { var projectRelativePath = VsPathUtilities.GetProjectFileRelativeToSolutionDirectoryPath(solutionFilePath, projectFilePath); var hasProjectReference = solutionFile.HasProjectReference(projectRelativePath, out var projectReference); if (!hasProjectReference) { return; } // Is the project reference in a nested solution folder? var hasNestedProjectsGlobalSection = solutionFile.GlobalSections.HasNestedProjectsGlobalSection(out var nestedProjectsGlobalSection); if (hasNestedProjectsGlobalSection) { nestedProjectsGlobalSection.ProjectNestings.RemoveAll(x => x.ProjectGUID == projectReference.ProjectGUID); } // Remove the project configuration platform entries. var hasProjectConfigurationPlatformsGlobalSection = solutionFile.GlobalSections.HasProjectConfigurationPlatformsGlobalSection(out var projectConfigurationPlatformsGlobalSection); if (hasProjectConfigurationPlatformsGlobalSection) { projectConfigurationPlatformsGlobalSection.ProjectBuildConfigurationMappings.RemoveAll(x => x.ProjectGUID == projectReference.ProjectGUID); } // Remove the project reference. solutionFile.SolutionFileProjectReferences.Remove(projectReference); }
/// <summary> /// Adds a project reference to the solution's dependencies folder, but checks first to avoid adding duplicates. /// </summary> public static void AddProjectReferenceDependencyChecked(this SolutionFile solutionFile, SolutionFileProjectReference projectReference) { var hasProjectReference = solutionFile.HasProjectReference(projectReference); if (!hasProjectReference) { solutionFile.AddProjectReferenceAsDependency(projectReference); } }