English 中文(简体)
修改视频演播室解决方案和 PowerShell 的工程文件
原标题:Modifying Visual Studio solution and project files with PowerShell

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(Powershell)来部分实现流程自动化,

在一个理想的世界里,我可以做一些事情,比如:

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

我知道 http://visualstudioogallery.msdn.microsoft.com/67620d8c-93dd-4e57-aa86-c9404abd7b3/" rel="noreferr">PowerConsole (我尚未充分探讨过这一点) 用于编译视觉工作室。然而,文件数量很少,我不确定它是否真正满足了我的需要。

容易操作的解决方案和工程文件还有别的东西吗? 最好是在PowerShell, 但我愿意接受其他建议。

最佳回答

根据我的经验, 使用 PowerShell (来自视觉工作室内外) 操作视觉工作室解决方案的最简单方式是将项目文件作为 XML 装入, 并使用 PowerShell 来操作它 。

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

如果您在 NuGet 软件包管理器控制台中正在运行脚本, 您可以获得所有工程文件的路径, 如 :

PM> get-project -all | select -expand FileName
C:UsersMeDocumentsVisual Studio 10ProjectsMyProjectMyProject.csproj
C:UsersMeDocumentsVisual Studio 10ProjectsMyProjectMyProjectTests.csproj
PM>
问题回答

PowerShell 的编辑解决方案文件对您在空白空格中使用的字符选择 very , 使得文本替换自动化很困难 。

如果您用 PowerShell 重新编辑 VS 解决方案文件, 您很有可能也能访问dotnet CLI 。 我建议从 PowerShell 中引用dotnet CLI, 并使用内置 < code>sln 命令 :

用法示例:

# Create a solution, a console app, and two class libraries.
dotnet new sln -n mysolution
dotnet new console -o myapp
dotnet new classlib -o mylib1
dotnet new classlib -o mylib2

# Add the projects to the solution
# Use --solution-folder to organize the class libraries into a solution folder.
dotnet sln mysolution.sln add myappmyapp.csproj
dotnet sln mysolution.sln add mylib1mylib1.csproj --solution-folder mylibs
dotnet sln mysolution.sln add mylib2mylib2.csproj --solution-folder mylibs

从PowerShell中引用时,您甚至可以使用环球图案:

dotnet sln todo.sln add (ls -r **/*.csproj)

Docs & examples: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-sln





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...