Inserting Quotes in MSBuild Tasks
29 Apr 2007I’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=""c:\program files\some folder\myprogram.exe"" />
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=""c:\program files\some folder\myprogram.exe"" />
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