使用PowerShell修改Visual Studio解决方案和项目文件 [英] Modifying Visual Studio solution and project files with PowerShell

查看:85
本文介绍了使用PowerShell修改Visual Studio解决方案和项目文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在重组我们的源代码,将内容移动到新目录中 结构体.这会影响我们的Visual Studio解决方案和项目文件,在这些文件中,诸如程序集引用,可能的输出目录,构建前和构建后事件等之类的事物必须进行更新以反映我们的更改.

We are currently reorganizing our source code, moving stuff around in a new directory structure. This impacts our Visual Studio solution and project files, where things like assembly references, possibly output directories, pre and post build events, and so on ... must be updated to reflect our changes.

由于我们有许多解决方案和项目,所以我希望能够使用PowerShell来部分自动化该过程,并为VS提供PowerShell提供者"之类的东西:

Since we have many solutions and projects, my hope was to partly automate the process using PowerShell, with something like a PowerShell "provider" for VS:

在理想的世界中,我将能够执行以下操作:

In an ideal world, I would be able to do something like:

$MySolution.Projects["MyProject"].PostBuildEvent = "copy <this> to <that>"

我了解 PowerConsole (我还没有尚未完全探索)来编写Visual Studio脚本.但是,该文档很稀少,我不确定它是否真的满足我的需求.

I know about PowerConsole (which I haven't fully explored yet) for scripting Visual Studio. However, the documentation is scarce and I'm not sure it really covers my needs.

还有其他可轻松处理解决方案和项目文件的内容吗?最好在PowerShell中使用,但我愿意接受其他建议.

Anything else for easily manipulating solution and project files? Preferably in PowerShell, but I'm open to other suggestions.

推荐答案

以我的经验,使用PowerShell(从Visual Studio内部或外部)使用PowerShell操作Visual Studio解决方案的最简单方法是将项目文件加载为XML并使用使用PowerShell进行操作.

In my experience, the easiest way to manipulate Visual Studio solutions using PowerShell (from within or outside of Visual Studio) is to load the project file as XML and use PowerShell to manipulate it.

$proj = [xml](get-content Path\To\MyProject.csproj)
$proj.GetElementsByTagName("PostBuildEvent") | foreach {
    $_."#text" = 'echo "Hello, World!"'
}
$proj.Save("Path\To\MyProject.csproj")

如果您正在NuGet软件包管理器控制台中运行脚本,则可以像这样获取所有项目文件的路径:

If you're running your script in the NuGet Package Manager Console, you can get the paths to all of the project files like so:

PM> get-project -all | select -expand FileName
C:\Users\Me\Documents\Visual Studio 10\Projects\MyProject\MyProject.csproj
C:\Users\Me\Documents\Visual Studio 10\Projects\MyProject\MyProjectTests.csproj
PM>

这篇关于使用PowerShell修改Visual Studio解决方案和项目文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆