Answers: <p>I had a similar need and I did it as below. In my case it is archived and published differently but I think you get the main idea.</p>
  <pre class="lang-yaml prettyprint-override"><code>    - task: PowerShell@2
        displayName: 'Get version number'
        inputs:
          targetType: 'inline'
          script: |
           $VERSION=([xml](Get-Content **\MyApp.csproj)).Project.PropertyGroup.Version
           Write-Host(&quot;##vso[task.setvariable variable=VERSION]$VERSION&quot;)
      
      ... // heare I archive the built files.
      
      - task: PublishPipelineArtifact@1
        inputs:
          targetPath: '$(Build.ArtifactStagingDirectory)/MyApp_$(VERSION).zip'
          artifact: 'MyApp_$(VERSION)'
          publishLocation: 'pipeline'
  </code></pre>
  <p>In the first powershell task I get the version number from the project file and set it to a variable called <code>VERSION</code>. I learned it from this <a href="https://stackoverflow.com/a/56039297/275940">https://stackoverflow.com/a/56039297/275940</a> answer which
  has done the same for maven project.</p>
  <p>In the second task I used the variable to make correct file path.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-06-11T12:18:40.453Z
Answers: 2021-06-11T12:18:40.453Z
Answers: Ehsan
Body: <p>I have the following tasks in my yaml file in Azure DevOps
  It publishes my console application and zip it in a file</p>
  <pre><code>- task: DotNetCoreCLI@2
    inputs:
      command: 'publish'
      publishWebProjects: false
      projects: '**/MyApp.csproj'
      arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
  
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'
  </code></pre>
  <p>I set the project version directly on the project file in Visual Studio when I make any changes.
  Now I'm trying to get that version when I am publishing the project in AzureDevOps and customise the file name, lets say for a project version 1.2.1 the archived file output of the publish in azure should be MyApp-v1.2.1.zip
  Currently it only output file as MyApp.zip</p>
  <p>Not sure if even my approach towards versioning is correct, so appreciate any input!</p>
  
Tags: <azure-devops><azure-pipelines><yaml><azure-artifacts>
Title: Retrieving project version from csproj in Azure Pipeline .NET Core CLI task
UserDisplayName: user65248
ThreadText: # Retrieving project version from csproj in Azure Pipeline .NET Core CLI task
  
  (Asked by: username_0 on 2021-02-20)
  
  I have the following tasks in my yaml file in Azure DevOps
  It publishes my console application and zip it in a file
  
  ```
  - task: DotNetCoreCLI@2
    inputs:
      command: 'publish'
      publishWebProjects: false
      projects: '**/MyApp.csproj'
      arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
  
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'
  
  ```
  
  I set the project version directly on the project file in Visual Studio when I make any changes.
  Now I'm trying to get that version when I am publishing the project in AzureDevOps and customise the file name, lets say for a project version 1.2.1 the archived file output of the publish in azure should be MyApp-v1.2.1.zip
  Currently it only output file as MyApp.zip
  
  Not sure if even my approach towards versioning is correct, so appreciate any input!
  
  **Tags:** azure-devops, azure-pipelines, yaml, azure-artifacts
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-11. Score: 1
  
  I had a similar need and I did it as below. In my case it is archived and published differently but I think you get the main idea.
  
  ```yaml
      - task: PowerShell@2
        displayName: 'Get version number'
        inputs:
          targetType: 'inline'
          script: |
           $VERSION=([xml](Get-Content **\MyApp.csproj)).Project.PropertyGroup.Version
           Write-Host("##vso[task.setvariable variable=VERSION]$VERSION")
      
      ... // heare I archive the built files.
      
      - task: PublishPipelineArtifact@1
        inputs:
          targetPath: '$(Build.ArtifactStagingDirectory)/MyApp_$(VERSION).zip'
          artifact: 'MyApp_$(VERSION)'
          publishLocation: 'pipeline'
  
  ```
  
  In the first powershell task I get the version number from the project file and set it to a variable called `VERSION`. I learned it from this <https://stackoverflow.com/a/56039297/275940> answer which
  has done the same for maven project.
  
  In the second task I used the variable to make correct file path.
  
  
  ---
  
  