C# File CreateSymbolicLink() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.CreateSymbolicLink() creates a symbolic link (a symlink): a small file that points to another file. When you read or write the link, the operating system sends you to the target file.

You give it two paths. path is where the new link will be, and pathToTarget is the file the link points to. It returns a FileSystemInfo that describes the new link.

The method is available from .NET 6. It is a static method of System.IO.File. For a link to a folder, use Directory.CreateSymbolicLink().

Syntax

Pass the path of the new link first, then the path of the file it should point to.

C#
string target = Path.Combine(Path.GetTempPath(), "codetofun-target.txt");
string link = Path.Combine(Path.GetTempPath(), "codetofun-link.txt");

File.WriteAllText(target, "Hello, link!");
FileSystemInfo info = File.CreateSymbolicLink(link, target);

Console.WriteLine(File.ReadAllText(link));
CallWhat it does
File.CreateSymbolicLink(link, target)Creates link so that it points to target, and returns a FileSystemInfo for the link.
info.LinkTargetReads back the path the link points to.
File.ResolveLinkTarget(link, true)Follows the link (and any further links) and returns the final target.

The target does not have to exist when you create the link. A link to a missing file is called a dangling link, and reading it fails until the target is created.

Library declarations
C#
public static FileSystemInfo CreateSymbolicLink(string path, string pathToTarget);

Parameters

ParameterDescription
pathWhere to create the link. A file or link must not already exist at this path.
pathToTargetThe path the link points to. It can be absolute, or relative to the folder of the link.

Return Value

Return ValueDescription
FileSystemInfoAn object that describes the new link. In practice it is a FileInfo, and its LinkTarget property holds the target path.

C# Examples

Example 1 — Create a Link and Read Through It

Make a target file, link to it, and read the text through the link. A try/catch prints a friendly message when the computer does not allow symlinks.

C#
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-createsymboliclink");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string target = Path.Combine(root, "target.txt");
        string link = Path.Combine(root, "shortcut.txt");
        await File.WriteAllTextAsync(target, "Hello via link");

        try {
            FileSystemInfo info = File.CreateSymbolicLink(link, target);

            Console.WriteLine(await File.ReadAllTextAsync(link));
            Console.WriteLine(Path.GetFileName(info.LinkTarget));
            Console.WriteLine(info.Attributes.HasFlag(FileAttributes.ReparsePoint));
        } catch (Exception ex) when (ex is UnauthorizedAccessException || ex is IOException) {
            Console.WriteLine("Symbolic links need extra permission on this computer.");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. A real target. target.txt holds the text Hello via link.
  2. Reading the link reads the target. shortcut.txt has no text of its own. ReadAllTextAsync(link) follows it and returns the target's text.
  3. Inspect the link. LinkTarget holds the target path, and Path.GetFileName keeps only target.txt. The ReparsePoint flag is how .NET marks a link, so it prints True.

Example 2 — Write Through the Link

Writing to the link changes the target file. If Example 1 printed the permission message, enable Developer Mode on Windows or run the program as administrator first.

C#
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-createsymboliclink-write");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string target = Path.Combine(root, "target.txt");
        string link = Path.Combine(root, "shortcut.txt");
        await File.WriteAllTextAsync(target, "original");

        File.CreateSymbolicLink(link, target);

        await File.WriteAllTextAsync(link, "changed through link");

        Console.WriteLine(await File.ReadAllTextAsync(target));
        Console.WriteLine(new FileInfo(target).Length);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Start with original. The target holds the text original.
  2. Write to the link. WriteAllTextAsync(link, ...) opens the link, and the system opens the target instead. The old text is replaced.
  3. Read the target. The target now holds changed through link, which is twenty characters, so its length is 20.

Example 3 — Delete the Link, Keep the Target

Deleting a link removes only the link. The target file stays where it is.

C#
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-createsymboliclink-delete");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string target = Path.Combine(root, "target.txt");
        string link = Path.Combine(root, "shortcut.txt");
        await File.WriteAllTextAsync(target, "keep me");

        File.CreateSymbolicLink(link, target);
        Console.WriteLine(File.Exists(link));

        File.Delete(link);

        Console.WriteLine(File.Exists(link));
        Console.WriteLine(File.Exists(target));

        Directory.Delete(root, true);
    }
}

How It Works

  1. The link exists. File.Exists(link) prints True because the link points to a real file.
  2. Delete the link. File.Delete(link) removes the link itself, not the file it pointed to.
  3. Only the link is gone. The link no longer exists (False), but target.txt is still there (True).

Frequently Asked Questions

It creates a symbolic link, a small file that points to another file. Reading or writing the link works on the target file. It returns a FileSystemInfo that describes the new link.
The first, path, is where the new link is created. The second, pathToTarget, is the file the link points to.
Creating symbolic links on Windows needs Developer Mode turned on or a program run as administrator. Linux and macOS do not have this rule.
File.Delete on the link removes only the link. The target file is not touched.
Use Directory.CreateSymbolicLink(path, pathToTarget). File.CreateSymbolicLink is meant for links to files.

Next: CreateText()

Create or overwrite a text file and get a StreamWriter for writing lines.

CreateText() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful