Inserting Quotes in MSBuild Tasks

I’ve finally got around to cracking open the MSBuild documentation and writing a few custom scripts. If you program in Visual Studio 2005, you’re using MSBuild and like most things programming, its worth some time to understand the tool. It’s fairly straight-forward to learn and I won’t repeat the already very good tutorials available all over the Internet. I did run into one problem that proved irritatingly hard to resolve. Consider the following:

<Exec Command="c:\program files\some folder\myprogram.exe" />

When MSBuild tries to execute this task, you get an error saying it couldn’t execute “c:\program”. Obviously the spaces in the command are causing a problem here. The question is, how does one quote the command inside an XML file? My first thought was that since it is XML, I should be able to use the standard XML character entities.

<Exec Command="&quot;c:\program files\some folder\myprogram.exe&quot" />

Update (12/11/07): There’s a typo in my example as someone kindly pointed out. It actually does work to use " character entity:

<Exec Command="&quot;c:\program files\some folder\myprogram.exe&quot;" />
Nice idea, but it doesn't work. Same error, same problem.

After spending what seemed like half-of-day scouring forums, I finally found a clue. MSBuild recognizes URL encoding. After looking up what the ASCII value for a quote (its 22), I tried the following:

<Exec Command="%22c:\program files\some folder\myprogram.exe%22" />

Ugly as sin, but it works. Surely there has to be a better way to handle white-space in MSBuild projects?

Technorati tags: MSBuild, Visual Studio 2005, .NET

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Quick Tip - Clear ReSharper's Cache to Fix False Errors
Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
More... (1077)

Donate with PayPal