Raffaello
Raffaello

Reputation: 1706

How to create a symbolic link in C#/NET6.0 using FileInfo.CreateAsSymbolicLink?

Given this snippet running on Windows:

string filePath = "myfile";
string linkTarget = "myfile.link";

FileInfo finfo = new(filePath);
FileInfo fi = new(linkTarget);

finfo.Create().Close();
Assert.IsTrue(finfo.Exists);

fi.CreateAsSymbolicLink(filePath);
Assert.IsTrue(fi.Exists); // here thrown, assert is false. And LinkTarger == null.

the symbolic link is not being created and doesn't exists.

It looks like a .NET6.0 bug to me.

what's wrong with it?

What am I missing?

How can i create a symbolic link using FileInfo or related?

EDIT

I forgot that must be an admin to create a symbolic link in windows, besides shouldn't throw instead a "NotAuthorized" or something?

At this point if i want to create a symbolic link not as admin won't be possible then?

Upvotes: 1

Views: 920

Answers (1)

Raffaello
Raffaello

Reputation: 1706

Based on this other answer. The reason why the file doesn't exists is because the program doesn't run with admin privileges

It looks like there is a policy to set up to allow non admin to create symlinks.

Upvotes: 1

Related Questions