$SpecificSolutionName$ is always empty. How can I tell if the user is making a new solution directory or not?

后端 未结 3 852
滥情空心
滥情空心 2020-12-22 01:25

In Visual Studio 2017, creating a C# Project Template project with the IWizard interface, I pop up my customised dialog to the user but I can\'t determine whether they\'ve p

相关标签:
3条回答
  • 2020-12-22 02:07

    I learnt that I can just "Attach to Process... " the temporary instance of VS and debug that. So I got to have a look at the replacementsDictionary object.

    Contrary to what all the docs say, the template parameter required is actually:

    $SpecifiedSolutionName$

    ... and not "SpecificSolutionName".

    That's half the mystery solved but don't let this excite you. SpecifiedSolutionName also doesn't do what the docs say it does.

    From https://docs.microsoft.com/en-us/visualstudio/ide/template-parameters:

    When "create solution directory" is not checked, SpecificSolutionName [sic] is blank.

    Nope. When "create solution directory" is not checked, $SpecifiedSolutionName$ contains whatever is in $projectname$.

    That would be enough for us if it wasn't for the fact that it is default behaviour for a solution directory and project directory to have the same name. But since that's a common occurrence, this $SpecifiedSolutionName$ value can't tell us whether the user created a new solution directory or not.

    So I still see nothing that directly reports whether the user ticked or unticked that checkbox. There is some logic you can jump through however, thanks to another template parameter called $solutiondirectory$.

    Which is also broken.

    If the user ticks "Create directory for solution", then $solutiondirectory$ is the directory that will hold the solution file. If the user unticks "Create directory for solution", then $solutiondirectory$ is the directory that holds the directory that will hold the solution file, and is thus probably higher up the filesystem than you care about.

    What $solutiondirectory$ reports is actually just $destinationdirectory$\..\. That is, the directory above the directory that will hold the project file. It doesn't matter if "Create directory for solution" was ticked or not, the $solutiondirectory$ parameter cares about the project's file & directory, not the solution's.

    So back to the question, how do we know if the user has ticked "Create directory for solution", when solution and project might both have the same name?

    Two wrongs almost make a right in this case. Compare $SpecifiedSolutionName$ to the final path element in $solutiondirectory$. If these are different, the user has definitely unticked "Create directory for solution." (Because the former will be the project name, and the latter will be whatever the filesystem has a bit higher up.)

    If they are the same, the user has probably ticked that box. But there is one more thing to consider. Perhaps for some reason the directory above all of this also has the same name as the project and/or solution directory. Users can be odd like that. I don't know a sure-fire way to account for this situation (perhaps there are more directories with the same name), so I just leave it as something to be aware of.

    0 讨论(0)
  • 2020-12-22 02:08

    Here's what works in VS2019:

    When the user checks the 'Place solution and project in the same directory' checkbox, $specifiedsolutionname$ is empty, and $solutiondirectory$ contains the path to the root folder where everything related to the new solution is placed. When unchecked, $specifiedsolutionname$ contains a 'solution name' specified by the user (which is the name of the sub-folder created under the solution root folder, and where project(s) are placed), and $destinationdirectory$ contains the correct path to the solution root folder.

    HTH

    0 讨论(0)
  • 2020-12-22 02:21

    Finally, for a multi-project solution, here's what got me working:-

    Solution level (multi-project) template:-

    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink ProjectName="$safeprojectname$.WebApp">
                WebApp\MyTemplate.vstemplate
            </ProjectTemplateLink>   .....
    

    Code level changes

    namespace $safeprojectname$
    

    Due to the 1st step, the project name now includes the solution name. At project level, the namespace and the assembly name should be ideally like that, without any requirement to separate out there.

    Let me know if this helps

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