Setting the OutputPath property of a project via Visual Studio Automation

DoomMuffins picture DoomMuffins · Nov 29, 2013 · Viewed 16.8k times · Source

I'm writing a VSIX package to allow the user to bulk-edit the OutputPath property of all the active configurations of projects in the currently loaded solution (see the incredibly annoying step #4 here).

I ran into a very specific problem: when setting the property to a value containing macros (e.g. "$(SolutionDir)\bin\Debug" the value written into the .csproj is escaped as follows:

<OutputPath>%24%28SolutionDir%29\bin\Debug\</OutputPath>

Which, rather than letting MSBuild expand the macro, creates an actual physical folder named $(SolutionDir). I'd like to somehow bypass this escaping.

The MSDN documentation is unsurprisingly lacking in that area.

My initial code is as follows:

private void MenuItemCallback(object sender, EventArgs e)
{
    SolutionWideOutputDialogWindow dialog = new SolutionWideOutputDialogWindow();
    dialog.ShowModal();
    if (!dialog.DialogResult.HasValue || !dialog.DialogResult.Value)
    {
        return;
    }

    string requestedOutputPath = dialog.outputPathTextBox.Text;
    Solution2 solution = _dte2.Solution as Solution2;
    if (solution == null)
    {
        return;
    }

    Projects projects = solution.Projects;
    foreach (Project project in projects)
    {
        Property outputPath = project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath");
        outputPath.Value = requestedOutputPath;
        project.Save();
    }
}

Greatly appreciate anyone's help.

Answer

dejuknow picture dejuknow · Jul 3, 2014

Visual Studio will unfortunately escape special characters when editing from project properties.

To fix this, edit your .csproj file directly in a text editor.

For example, change:

<OutputPath>%24%28SolutionDir%29\bin\Debug\</OutputPath>

to:

<OutputPath>$(SolutionDir)\bin\Debug\</OutputPath>