【问题标题】:Save file to Azure DevOps folder like /home/vsts/work/1/s/ from C# console application从 C# 控制台应用程序将文件保存到 Azure DevOps 文件夹,例如 /home/vsts/work/1/s/
【发布时间】:2020-06-25 14:22:39
【问题描述】:

我正在尝试在 C# 控制台应用程序中进行一些处理,并且必须保存该文件:

File.WriteAllText("C:\JSONOutput\output.md"), htmlResult);

我想将它保存到 Azure DevOps 文件夹,如“/home/vsts/work/1/s/”,以便在下一步中再次访问它,我将使用如下语句将文件上传到工件:

Write-Host "##vso[artifact.upload containerfolder=testresult;artifactname=worksoftHTML;]/home/vsts/work/1/s/output.html"

到目前为止,我已经尝试编写以下内容,但它不起作用:

File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"/home/vsts/work/1/s/output.md"), htmlResult);

还有这个:

File.WriteAllText("../home/vsts/work/1/s/output.md", htmlResult);

它也不起作用。请帮助。 我在管道中也尝试了以下方法,我将文件路径作为参数传递给命令行中的程序:

- task: CmdLine@2 
  env:     
      InputJSON: $(testJSON)  
  inputs:  
    script: 
        echo $(testJSON)               
        'jsonProcessor.exe $(Build.ArtifactStagingDirectory)\dropfile\,%INPUTJSON%'     
    workingDirectory: './jsonProcExe/'

在 exe 中,我正在编写如下文件:

File.WriteAllText(Path.Combine(filePath, @"output.html"), finalHtml);

然后在下一步中,我正在读取这样的文件:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.      
      Write-Host "##vso[artifact.upload containerfolder=testresult;artifactname=worksoftHTML;]$(Build.ArtifactStagingDirectory)\dropfile\output.html"

但我仍然收到错误

Path does not exist: C:\agent\_work\1\a\dropfile\output.html

请注意,我使用的是自托管代理。

【问题讨论】:

    标签: azure azure-devops


    【解决方案1】:

    我的管道中的以下行不正确:

    jsonProcessor.exe $(Build.ArtifactStagingDirectory)\dropfile\,%INPUTJSON%
    

    因为这不是您应该从命令行向控制台应用程序传递参数的方式。此外,我试图在上面的行中将 JSON 作为参数传递。那不管用。所以以下是我所做的: 我正在从 Build.StagingArtifact 文件夹中的 powershell 脚本中保存我的 JSON:

    Write-Output $json | Out-File $env:BUILD_ARTIFACTSTAGINGDIRECTORY/results.json
    

    然后在命令行任务中,我通过以下方式传递上述文件夹位置:

    task: CmdLine@2     
      inputs:  
        script:                                
            'jsonProcessor.exe $(Build.ArtifactStagingDirectory)\results.json $(Build.ArtifactStagingDirectory)'    
        workingDirectory: './jsonProcExe/'
    

    同样在 .exe 文件的 c# 代码中,最好通过将反斜杠替换为前斜杠来修改从命令行传递的文件路径。 然后我可以使用以下代码行从 C# exe 编写文件:

    //Create the HTML file.
    File.WriteAllText(filePath + "/output.html", finalHtml);
    

    【讨论】:

    • 太棒了!感谢您在这里分享您的解决方案,您可以接受它作为答案,这样可以帮助遇到相同问题的其他社区成员。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-07-28
    • 2020-03-30
    • 2020-11-06
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多