C# 以编程方式修改项目文件内的DLL引用
我需要强制所有项目使用存储在公共位置的C# 以编程方式修改项目文件内的DLL引用,c#,reference,msbuild,csproj,project-files,C#,Reference,Msbuild,Csproj,Project Files,我需要强制所有项目使用存储在公共位置的dll。我首先重构现有项目,指向我喜欢的dll的源文件夹。所以我决定尝试一种编程方式来更改这些引用 我阅读了dll参考资料如下: XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003"; XDocument projDefinition = XDocument.Load(filePath); IEnumerable&
dll
。我首先重构现有项目,指向我喜欢的dll的
源文件夹。所以我决定尝试一种编程方式来更改这些引用
我阅读了dll
参考资料如下:
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(filePath);
IEnumerable<string> references = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "Reference")
.Elements(msbuild + "HintPath")
.Select(refElem => refElem.Value);
XNamespace msbuild=”http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition=XDocument.Load(文件路径);
IEnumerable references=projDefinition
.Element(msbuild+“项目”)
.Elements(msbuild+“项目组”)
.Elements(msbuild+“引用”)
.Elements(msbuild+“HintPath”)
.选择(refElem=>refElem.Value);
下一步是根据引用名称更改引用HintPath
。我计划创建一个引用字典,然后遍历它们以更新每个项目文件
但是,我找不到任何可靠的方法来读取和更改
HintPath
基于参考内部csproj
我可能已经很晚了,我还没有测试这个特定的应用程序,但您应该能够获取您拥有的代码并删除。选择(refElem=>refElem.Value)
在迭代引用之前
这将允许您访问元素本身,并允许更新元素的值。一旦更改了所有需要更改的值,就可以将XMLdoc写回proj文件
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(filePath);
IEnumerable<string> references = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "Reference")
.Elements(msbuild + "HintPath");
foreach(var element in references)
{
// do what you need to in order to update the reference
}
projDefinition.Save(filePath);
XNamespace msbuild=”http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition=XDocument.Load(文件路径);
IEnumerable references=projDefinition
.Element(msbuild+“项目”)
.Elements(msbuild+“项目组”)
.Elements(msbuild+“引用”)
.元素(msbuild+“HintPath”);
foreach(引用中的var元素)
{
//执行更新引用所需的操作
}
projDefinition.Save(文件路径);
我可能已经很晚了,我还没有测试这个特定的应用程序,但是您应该能够获取您拥有的代码并删除。在迭代引用之前选择(refElem=>refElem.Value)
这将允许您访问元素本身,并允许更新元素的值。一旦更改了所有需要更改的值,就可以将XMLdoc写回proj文件
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(filePath);
IEnumerable<string> references = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "Reference")
.Elements(msbuild + "HintPath");
foreach(var element in references)
{
// do what you need to in order to update the reference
}
projDefinition.Save(filePath);
XNamespace msbuild=”http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition=XDocument.Load(文件路径);
IEnumerable references=projDefinition
.Element(msbuild+“项目”)
.Elements(msbuild+“项目组”)
.Elements(msbuild+“引用”)
.元素(msbuild+“HintPath”);
foreach(引用中的var元素)
{
//执行更新引用所需的操作
}
projDefinition.Save(文件路径);
查看一个类似问题的答案:答案准确地显示了我所做的事情,并且它会转储读取到txt的引用。不是很有用。我需要的是能够根据引用修改HintPath。请查看类似问题的答案:答案完全显示了我所做的事情,并且它会将读取的引用转储到txt。不是很有用。我需要的是能够根据参考修改HintPath。