public virtual void AddReference(string referencePath, Stream stream) { string name = Path.GetFileNameWithoutExtension(referencePath); try { // Get the full path to the reference string fullPath = PathUtility.GetAbsolutePath(Root, referencePath); // Add a reference to the project Reference reference = Project.Object.References.Add(fullPath); // Always set copy local to true for references that we add reference.CopyLocal = true; // This happens if the assembly appears in any of the search paths that VS uses to locate assembly references. // Most commmonly, it happens if this assembly is in the GAC or in the output path. if (!reference.Path.Equals(fullPath, StringComparison.OrdinalIgnoreCase)) { // Get the msbuild project for this project MsBuildProject buildProject = Project.AsMSBuildProject(); if (buildProject != null) { // Get the assembly name of the reference we are trying to add AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath); // Try to find the item for the assembly name MsBuildProjectItem item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences() where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2) select assemblyReferenceNode.Item1).FirstOrDefault(); if (item != null) { // Add the <HintPath> metadata item as a relative path item.SetMetadataValue("HintPath", referencePath); // Save the project after we've modified it. Project.Save(); } } } Logger.Log(MessageLevel.Debug, VsResources.Debug_AddReference, name, ProjectName); } catch (Exception e) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, VsResources.FailedToAddReference, name), e); } }
public virtual void AddReference(string referencePath, Stream stream) { string name = Path.GetFileNameWithoutExtension(referencePath); try { // Get the full path to the reference string fullPath = PathUtility.GetAbsolutePath(Root, referencePath); string assemblyPath = fullPath; bool usedTempFile = false; // There is a bug in Visual Studio whereby if the fullPath contains a comma, // then calling Project.Object.References.Add() on it will throw a COM exception. // To work around it, we copy the assembly into temp folder and add reference to the copied assembly if (fullPath.Contains(",")) { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(fullPath)); File.Copy(fullPath, tempFile, true); assemblyPath = tempFile; usedTempFile = true; } // Add a reference to the project Reference reference = Project.Object.References.Add(assemblyPath); // if we copied the assembly to temp folder earlier, delete it now since we no longer need it. if (usedTempFile) { try { File.Delete(assemblyPath); } catch { // don't care if we fail to delete a temp file } } TrySetCopyLocal(reference); // This happens if the assembly appears in any of the search paths that VS uses to locate assembly references. // Most commonly, it happens if this assembly is in the GAC or in the output path. if (!reference.Path.Equals(fullPath, StringComparison.OrdinalIgnoreCase)) { // Get the msbuild project for this project MsBuildProject buildProject = Project.AsMSBuildProject(); if (buildProject != null) { // Get the assembly name of the reference we are trying to add AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath); // Try to find the item for the assembly name MsBuildProjectItem item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences() where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2) select assemblyReferenceNode.Item1).FirstOrDefault(); if (item != null) { // Add the <HintPath> metadata item as a relative path item.SetMetadataValue("HintPath", referencePath); // Save the project after we've modified it. Project.Save(); } } } Logger.Log(MessageLevel.Debug, VsResources.Debug_AddReference, name, ProjectName); } catch (Exception e) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, VsResources.FailedToAddReference, name), e); } }
public virtual void AddReference(string referencePath) { if (referencePath == null) { throw new ArgumentNullException("referencePath"); } string name = Path.GetFileNameWithoutExtension(referencePath); try { // Get the full path to the reference string fullPath = Path.Combine(ProjectFullPath, referencePath); string assemblyPath = fullPath; bool usedTempFile = false; // There is a bug in Visual Studio whereby if the fullPath contains a comma, // then calling Project.Object.References.Add() on it will throw a COM exception. // To work around it, we copy the assembly into temp folder and add reference to the copied assembly if (fullPath.Contains(",")) { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(fullPath)); File.Copy(fullPath, tempFile, true); assemblyPath = tempFile; usedTempFile = true; } // Add a reference to the project dynamic reference = EnvDTEProjectUtility.GetReferences(EnvDTEProject).Add(assemblyPath); // if we copied the assembly to temp folder earlier, delete it now since we no longer need it. if (usedTempFile) { try { File.Delete(assemblyPath); } catch { // don't care if we fail to delete a temp file } } if (reference != null) { var path = GetReferencePath(reference); // If path != fullPath, we need to set CopyLocal thru msbuild by setting Private // to true. // This happens if the assembly appears in any of the search paths that VS uses to // locate assembly references. // Most commonly, it happens if this assembly is in the GAC or in the output path. if (path != null && !path.Equals(fullPath, StringComparison.OrdinalIgnoreCase)) { // Get the msbuild project for this project MicrosoftBuildEvaluationProject buildProject = EnvDTEProjectUtility.AsMicrosoftBuildEvaluationProject(EnvDTEProject); if (buildProject != null) { // Get the assembly name of the reference we are trying to add AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath); // Try to find the item for the assembly name MicrosoftBuildEvaluationProjectItem item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences() where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2) select assemblyReferenceNode.Item1).FirstOrDefault(); if (item != null) { // Add the <HintPath> metadata item as a relative path item.SetMetadataValue("HintPath", referencePath); // Set <Private> to true item.SetMetadataValue("Private", "True"); // Save the project after we've modified it. FileSystemUtility.MakeWriteable(EnvDTEProject.FullName); EnvDTEProject.Save(); } } } else { TrySetCopyLocal(reference); TrySetSpecificVersion(reference); } } NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_AddReference, name, ProjectName); } catch (Exception e) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Strings.FailedToAddReference, name), e); } }