is there a cross platform way to create a symbolic link in .NET Core
Asked Answered
C

1

6

I can use the following p-invoke code on Windows, but is there a cross-platform way to create a symbolic link with .NET Core? (3.0 preview if that matters)

If not, is there a way to p-invoke or similar on linux/ubuntu to do it?

public static class PinvokeSymLink
{
    [DllImport( "kernel32.dll" )]
    public static extern bool CreateSymbolicLink( string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags );

    public enum SymbolicLink
    {
        File = 0,
        Directory = 1
    }
}
Commove answered 28/8, 2019 at 13:34 Comment(6)
I also stumbled on this context, I need symlinks for external HD references.Levinson
@Levinson I think the only way may be to just shell out 'ln -s directory simlin'Commove
@Commove did you found the solutionDelacourt
@Delacourt I used the shell command in the comment above.Commove
@Kenny thanks I found the dll Mono.Unix dll with the help of that I was able to achieve Mono.Unix.UnixSymbolicLinkInfoDelacourt
@Delacourt great ... make it an answer and I'll vote and select it.Commove
H
0

Since .NET 6, the File.CreateSymbolicLink is available for this purpose.

For the older .NET versions, you can consider p-invoking the symlink Linux function instead. This worked for me on both Linux and macOS.

Harpist answered 20/7, 2022 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.