/// <summary>
        /// Adds the provided Reference to the received Project.
        /// </summary>
        /// <param name="targetProject">Project to add the Reference.</param>
        /// <param name="referencedProject">Project to be referenced.</param>
        public static void AddReferenceToProject(Project targetProject, Project referencedProject)
        {
            if (targetProject.FullName == referencedProject.FullName)
            {
                return;
            }

            if (targetProject.Object is VSLangProj.VSProject)
            {
                VSLangProj.VSProject vsproject = (VSLangProj.VSProject)targetProject.Object;
                VSLangProj.Reference reference = null;

                try
                {
                    reference = vsproject.References.Find(referencedProject.Name);
                }
                catch (Exception)
                {
                    // It failed to find one, so it must not exist.
                }

                if (reference == null)
                {
                    try
                    {
                        vsproject.References.AddProject(referencedProject);
                    }
                    catch (Exception)
                    {
                        // Add warning to ErrorList pane
                        VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning,
                                                          string.Format(Resources.Warning_ManuallyAddReference, referencedProject.Name, targetProject.Name),
                                                          targetProject, null, null, null);
                    }
                }
                else
                {
                    // Reference already exists.
                }
            }
            else if (targetProject.Object is VsWebSite.VSWebSite)
            {
                VsWebSite.VSWebSite         vswebsite = (VsWebSite.VSWebSite)targetProject.Object;
                VsWebSite.AssemblyReference reference = null;

                try
                {
                    foreach (VsWebSite.AssemblyReference r in vswebsite.References)
                    {
                        if (r.Name == referencedProject.Name)
                        {
                            reference = r;
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    // It failed to find one, so it must not exist.
                }

                if (reference == null)
                {
                    try
                    {
                        vswebsite.References.AddFromProject(referencedProject);
                    }
                    catch (Exception)
                    {
                        // Add warning to ErrorList pane
                        VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning,
                                                          string.Format(Resources.Warning_ManuallyAddReference, referencedProject.Name, targetProject.Name),
                                                          targetProject, null, null, null);
                    }
                }
                else
                {
                    // Reference already exists.
                }
            }
            else
            {
                // Add warning to ErrorList pane
                VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning,
                                                  string.Format(Resources.Warning_ManuallyAddReference, referencedProject.Name, targetProject.Name),
                                                  targetProject, null, null, null);
            }
        }
Exemple #2
0
        /// <summary>
        /// Adds a reference to the selected project.
        /// </summary>
        /// <remarks>
        /// REF=http://www.codeproject.com/KB/macros/EnvDTE.aspx
        /// </remarks>
        /// <param name="project"></param>
        /// <param name="referenceStrIdentity"></param>
        /// <param name="browseUrl"></param>
        public static void AddProjectReference(Project project, string referenceStrIdentity, string browseUrl)
        {
            //browseUrl is either the File Path or the Strong Name
            //(System.Configuration, Version=2.0.0.0, Culture=neutral,
            //                       PublicKeyToken=B03F5F7F11D50A3A)
            string path = "";

            if (!browseUrl.StartsWith(referenceStrIdentity))
            {
                //it is a path
                path = browseUrl;
            }

            if (project.Object is VSLangProj.VSProject)
            {
                VSLangProj.VSProject vsproject = (VSLangProj.VSProject)project.Object;
                VSLangProj.Reference reference = null;
                try
                {
                    reference = vsproject.References.Find(referenceStrIdentity);
                }
                catch (Exception)
                {
                    //it failed to find one, so it must not exist.
                    //But it decided to error for the fun of it. :)
                }
                if (reference == null)
                {
                    if (path == "")
                    {
                        vsproject.References.Add(browseUrl);
                    }
                    else
                    {
                        vsproject.References.Add(path);
                    }
                }
                else
                {
                    throw new Exception("Reference already exists.");
                }
            }
            else if (project.Object is VsWebSite.VSWebSite)
            {
                VsWebSite.VSWebSite         vswebsite = (VsWebSite.VSWebSite)project.Object;
                VsWebSite.AssemblyReference reference = null;
                try
                {
                    foreach (VsWebSite.AssemblyReference r in vswebsite.References)
                    {
                        if (r.Name == referenceStrIdentity)
                        {
                            reference = r;
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    //it failed to find one, so it must not exist.
                    //But it decided to error for the fun of it. :)
                }
                if (reference == null)
                {
                    if (path == "")
                    {
                        vswebsite.References.AddFromGAC(browseUrl);
                    }
                    else
                    {
                        vswebsite.References.AddFromFile(path);
                    }
                }
                else
                {
                    throw new Exception("Reference already exists.");
                }
            }
            else
            {
                throw new Exception("Currently, system is only set up " +
                                    "to do references for normal projects.");
            }
        }
        /// <summary>
        /// Adds the provided Reference to the received Project.
        /// </summary>
        /// <param name="targetProject">Project to add the Reference.</param>
        /// <param name="referenceIdentity">Reference Identity (Name).</param>
        /// <param name="browseUrl">URL of the Reference (same as the Identity if it is in the GAC).</param>
        public static void AddReferenceToProject(Project targetProject,
                                                 string referenceIdentity, string browseUrl)
        {
            string path = string.Empty;

            if (browseUrl.StartsWith(referenceIdentity) == false)
            {
                // It is a path
                path = browseUrl;
            }

            if (targetProject.Object is VSLangProj.VSProject)
            {
                VSLangProj.VSProject vsproject = (VSLangProj.VSProject)targetProject.Object;
                VSLangProj.Reference reference = null;

                try
                {
                    reference = vsproject.References.Find(referenceIdentity);
                }
                catch (Exception)
                {
                    // It failed to find one, so it must not exist.
                }

                if (reference == null)
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(path))
                        {
                            vsproject.References.Add(browseUrl);
                        }
                        else
                        {
                            vsproject.References.Add(path);
                        }
                    }
                    catch (Exception)
                    {
                        // Add warning to ErrorList pane
                        VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning,
                                                          string.Format(Resources.Warning_ManuallyAddReference, referenceIdentity, targetProject.Name),
                                                          targetProject, null, null, null);
                    }
                }
                else
                {
                    // Reference already exists.
                }
            }
            else if (targetProject.Object is VsWebSite.VSWebSite)
            {
                VsWebSite.VSWebSite         vswebsite = (VsWebSite.VSWebSite)targetProject.Object;
                VsWebSite.AssemblyReference reference = null;

                try
                {
                    foreach (VsWebSite.AssemblyReference r in vswebsite.References)
                    {
                        if (r.Name == referenceIdentity)
                        {
                            reference = r;
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    // It failed to find one, so it must not exist.
                }

                if (reference == null)
                {
                    if (string.IsNullOrWhiteSpace(path))
                    {
                        vswebsite.References.AddFromGAC(browseUrl);
                    }
                    else
                    {
                        vswebsite.References.AddFromFile(path);
                    }
                }
                else
                {
                    // Reference already exists.
                }
            }
            else
            {
                // Add warning to ErrorList pane
                VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning,
                                                  string.Format(Resources.Warning_ManuallyAddReference, referenceIdentity, targetProject.Name),
                                                  targetProject, null, null, null);
            }
        }