【问题标题】:How do I set the Start Action (external program) used by csproj file from powershell如何从 powershell 设置 csproj 文件使用的启动操作(外部程序)
【发布时间】:2015-03-09 09:57:15
【问题描述】:

我正在 powershell 中编写一个 nuget 安装脚本,我想为项目文件 (*.csproj) 设置“启动操作”。我可以通过变量 $project 从 EnvDTE 访问 Project 接口。

我知道手动设置“启动操作”会在文件系统中创建一个本地 *.user 文件,我是否可以通过 EnvDTE 从 powershell 脚本以编程方式执行此操作?

【问题讨论】:

    标签: powershell nuget envdte


    【解决方案1】:

    这就是我实现它的方式:

    param($installPath, $toolsPath, $package, $project)
    
    function HasStartAction ($item)
    {
        foreach ($property in $item.Properties)
        {
           if ($property.Name -eq "StartAction")
           {
               return $true
           }            
        } 
    
        return $false
    }
    
    function ModifyConfigurations
    {
        $configurationManager = $project.ConfigurationManager
    
        foreach ($name in $configurationManager.ConfigurationRowNames)
        {
            $projectConfigurations = $configurationManager.ConfigurationRow($name)
    
            foreach ($projectConfiguration in $projectConfigurations)
            {                
    
                if (HasStartAction $projectConfiguration)
                {
                    $newStartAction = 1
                    $newStartProgram = $fullPath + "bin\" + $name + "\Shell\XXXX.exe"
                    $newWorkingDirectory = $fullPath + "bin\" + $name + "\Shell\"
    
                    write-host "StartAction - " $newStartAction
                    write-host "StartProgram - " $newStartProgram
                    write-host "WorkingDirectory - " $newWorkingDirectory
    
                    $projectConfiguration.Properties.Item("StartAction").Value = $newStartAction
                    $projectConfiguration.Properties.Item("StartProgram").Value = $newStartProgram
                    $projectConfiguration.Properties.Item("StartWorkingDirectory").Value = $newWorkingDirectory
                }
            }
        }
    
        $project.Save
    }
    
    write-host "Modifying Configurations..."
    ModifyConfigurations
    

    【讨论】:

    • 在 VS2015 中,我必须在 $newStartProgram 分配前添加一个 [String] 以使其成功运行。但是,除此之外,它就像一个魅力。
    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2014-05-29
    • 2019-07-05
    • 1970-01-01
    • 2013-04-12
    相关资源
    最近更新 更多