Prebuild event in Visual Studio replacing $(SolutionDir) with *Undefined*

前端 未结 5 1966
猫巷女王i
猫巷女王i 2020-12-17 08:31

I believe the problem is documented here moved here and looks like it might be a bug in visual studio, but I\'m wondering if anyone knows of a workaround.

相关标签:
5条回答
  • 2020-12-17 08:55

    I fixed this by replacing all $(SolutionDir) with $(ProjectDir)..\.

    It was due to MSBuild running each project independently, and thus not the solution. It worked fine in VS2010, but not on the build server.

    0 讨论(0)
  • 2020-12-17 08:58

    Wasted a lot of time to find perfect solution for this problem. Use Directory.Build.props.

    In your sln location folder create a file with name Directory.Build.props and put this code inside:

    <Project>
     <PropertyGroup>
        <SolutionDir>$(MSBuildThisFileDirectory)</SolutionDir>
     </PropertyGroup>
    </Project>
    

    This file will be automagically picked up by all your csproj files and will define (SolutionDir) property.

    0 讨论(0)
  • 2020-12-17 09:00

    If you are using only MSBuild and SolutionDir works for some projects but for others not, it's possible that SolutionDir is already defined somewhere in .csproj file of the working projects.

    0 讨论(0)
  • 2020-12-17 09:05

    Allan's answer is exactly on the right path, but if you want to move to parent directory of the ProjectDir, or the $(ProjectDir)..\ output is going to be a string, you need to perform the 'go to parent directory' operation in msbuild.
    Here is how you can achieve this:

    <PropertyGroup>
      <TrimmedDir>$([System.IO.Path]::GetDirectoryName($(ProjectDir)))</TrimmedDir>
    </PropertyGroup>
    

    This code will set the parent directory path of the ProjectDir to TrimmedDir variable. [System.IO.Path]::GetDirectoryName() performs this operation.

    0 讨论(0)
  • 2020-12-17 09:06

    You don't specify if this happens for many projects or just one or two.

    If it is only in one or two projects, a temporary workaround might be to replace $(SolutionDir) with the explicit path that Folder2 is located in. For example, something like:

    "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "C:\AllSolutions\ExampleSolutions\Folder2\Folder3\Project2.csproj"
    

    but with the correct path for your project.

    0 讨论(0)
提交回复
热议问题