我认为我不能只设置多个启动项目的原因是它们必须按一定顺序启动,并且每个项目都必须在下一个项目启动之前完全初始化,因此我需要延迟。
我一直在使用 PowerShell,但我就是不知道如何启动特定项目。
现在我的脚本如下所示: '''
# Load the Visual Studio DTE COM object
$visualStudio = [Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.17.0") # Use 17.0 for VS2022
if ($null -eq $visualStudio) {
Write-Host "No active Visual Studio instance found!"
exit
}
# Path to the solution
$solution = $visualStudio.Solution
if ($solution.IsOpen) {
Write-Host "Solution is open: $($solution.FullName)"
# Get the projects in the solution
$projects = $solution.Projects
# Define the list of project names you want to start
$desiredProjects = @(
"Proj1",
"Proj2",
"Proj3"
)
foreach ($desiredProjectName in $desiredProjects) {
$projectFound = $null
# Loop through each project to find by name
for ($i = 1; $i -le $projects.Count; $i++) {
$project = $projects.Item($i)
Write-Host "Checking Project: $($project.Name)"
if ($project.Name -eq $desiredProjectName) {
$project.DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance")
Write-Host "Found project: $($project.Name)"
Write-Host "Project Type: $($project.Properties.Item('OutputType').Value)"
# Set the project as the startup project
# $solution.Properties.Item("StartupProject").Value = $project.UniqueName
# Write-Host "Set startup project to: $($project.Name)"
# Start debugging
Write-Host "Started debugging for project: $($project.Name)"
# Optional: Add delay before starting the next project
Start-Sleep -Seconds 5 # Delay of 5 seconds (adjust as needed)
break
}
}
}
'''
问题是,这总是只启动我设置为启动项目的任何内容。我希望它按顺序启动列表中的项目,延迟 5 秒。
我尝试过更改启动项目,但是一旦开始调试,就不再允许这样做。
那么有没有办法从同一个 Visual Studio 实例启动这三个工具?
探索了更长时间并找到了解决方案,如果其他人遇到此问题并想做类似的事情,这就是我发现的
似乎不可能像从上下文菜单中“调试>启动新实例”那样实际启动多个项目。
我最终所做的是启动三个可执行文件并将调试服务附加到它们。
# Paths to the project executables
$projects = @(
"path\to\proj1\Proj1.exe",
"path\to\Proj2\Proj2.exe",
"path\to\Proj3\Proj3.exe"
)
foreach ($project in $projects) {
$process = Start-Process $project -PassThru
# Attach the debugger
$processId = $process.Id
Write-Host "ProcessId: $($processId)"
$visualStudio = [Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.17.0")
# Get the list of local processes available for debugging
$processes = $visualStudio.Debugger.LocalProcesses
# Loop through the processes to find the one with the matching Process ID
foreach ($runningProcess in $processes) {
if ($runningProcess.ProcessID -eq $processId) {
Write-Host "Attaching debugger to process: $($process.Name) (PID: $($process.ProcessID))"
$runningProcess.Attach() # Attach the debugger to the process
Write-Host "Debugger attached to process: $($process.Name)"
}
}
Start-Sleep -Seconds 5
}