c# – How to use dotnet 6 functions in msbuild inline tasks?


I have Visual Studio 2022, and I have dotnet 9 installed.

During build, (before the build starts, actually,) I need to create a symbolic link, and I need to do so in a cross-platform way, because I am on windows, but github actions runs ubuntu.

So, I created an “Inline Task”, a.k.a. “UsingTask” (https://learn.microsoft.com/en-us/visualstudio/msbuild/usingtask-element-msbuild?view=vs-2022) from which I want to invoke System.IO.File.CreateSymbolicLink(). (https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createsymboliclink?view=net-6.0)

Here is my code:

<Project Sdk="Microsoft.NET.Sdk">
    ...
    <UsingTask TaskName="CreateFileSymbolicLink" 
            TaskFactory="RoslynCodeTaskFactory" 
            AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
        <ParameterGroup>
            <path ParameterType="System.String" Required="true" />
            <pathToTarget ParameterType="System.String" Required="true" />
        </ParameterGroup>
        <Task>
            <Code Type="Fragment" Language="cs">
                //System.Console.WriteLine( $"x" );
                System.IO.File.CreateSymbolicLink( path, pathToTarget );
            </Code>
        </Task>
    </UsingTask>

It fails with

 error CS0117: 'File' does not contain a definition for 'CreateSymbolicLink'.

I found this: https://stackoverflow.com/a/51194833/773113 (from 2018) and I tried using the string interpolation syntax, and that one works, but not the CreateSymbolicLink() method.

How can I get this to work, or otherwise achieve my original goal, which was to create a symbolic link to a file in a cross-platform way before build begins?

Leave a Reply

Your email address will not be published. Required fields are marked *