Visual Studio扩展:更改程序集引用的提示路径

时间:2017-04-01 20:06:34

标签: c# visual-studio visual-studio-extensions envdte

我正在编写Visual Studio扩展,我想更改C#项目的程序集引用的提示路径,而不会触发"检测到文件修改" -dialog。

<Reference Include="SomeAssembly">
  <HintPath>C:\ChangeMe\SomeAssembly.dll</HintPath>
</Reference>

但是在VSLangProj110.Reference5-interface中,我找不到任何可以使用的属性。 (通过VSLangProj140.VSProject3.References访问)

2 个答案:

答案 0 :(得分:1)

Microsoft.Build.BuildEngine.Project已过时。这是一个更新的工作解决方案。

foreach (var dteProject in dte.Solution.Projects.OfType<Project>())
{
    // You can edit the project through an object of Microsoft.Build.Evaluation.Project 
    var buildProject = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(dteProject.FullName).First();
    foreach (var item in buildProject.Items.Where(obj => obj.ItemType == "Reference"))
    {
        var newPath = SomeMethod(item.GetMetadata("HintPath"));

        item.SetMetadataValue("HintPath", newPath);
    }

    // But you have to save through an object of EnvDTE.Project
    dteProject.Save();
}

答案 1 :(得分:0)

我创建了一个演示并在我身边重现了你的问题。我认为这是一个设计问题,如果你在环境之外修改项目,它会弹出“File Modification Detected”对话框,我们需要手动更改它。

您可以在以下链接上发布反馈:https://connect.microsoft.com/VisualStudio/Feedback

更新

DTE2 dte = (DTE2)this.ServiceProvider.GetService(typeof(DTE));
            EnvDTE.Project currentProject = dte.Solution.Projects.Item(1);

            // Create a new Project object.
            Microsoft.Build.BuildEngine.Project project = new Microsoft.Build.BuildEngine.Project();

            project.Load(currentProject.FullName);

            foreach (BuildItemGroup ig in project.ItemGroups)
            {
                //var items = ig.ToArray();

                foreach (BuildItem item in ig.ToArray())
                {
                    if (item.Include == "ClassLibrary1")
                    {
                        item.Include = "Utils";
                        item.SetMetadata("HintPath", @"C:\relativePath\Utils.dll");
                    }
                }
            }
            project.Save(currentProject.FullName);