18

I need to replace characters in a variable I am passing to an exec task within msbuild 4. Specifically, I need to replace all occurrences of backslashes \ with forward-slashes / in the $(MSBuildProjectDirectory) variable.

eg:

<Target Name="DoStuff">
    <Exec Command="stuff.exe $(MSBuildProjectDirectoryWithSlashesFixed)/SomeFile.txt" />
</Target>

The executable being called is an oracle component that can't deal with slashes in windows format.

I've had a look at Property Functions, but as System.String.Replace() is an instance method rather than a static method, it seems it can't be used for my needs.

Any ideas?

grenade
  • 28,964
  • 22
  • 90
  • 125

1 Answers1

32

You can also use instance methods as property functions (as long as you restrict yourself to types registered as safe).

<PropertyGroup>
  <MSBuildProjectDirectoryWSF>$(MSBuildProjectDirectory.Replace('\', '/'))</MSBuildProjectDirectoryWSF>
</PropertyGroup>

(I might have missed some escaping on the slashes).

More documentation is available on Visual Studio Blog.

Ian Kemp
  • 24,155
  • 16
  • 97
  • 121
skolima
  • 29,517
  • 26
  • 108
  • 146