
Symbolic Links (often abbreviated as symlinks) are a type of file or folder shortcut in Windows that reference another file or folder in the file system, rather than copying its contents. Unlike a normal shortcut, a symlink acts as if it were the original file or folder, so any changes to the symlink will affect the original file and vice versa.
Symlinks can be used to create shortcuts to files or folders that are located on another drive, in another location on the same drive or even on a network location. They can also be used to redirect files or folders to a new location or to create multiple references to the same file or folder.
.NET has no built-in API to create symbolic links on Windows. However, it is possible to create symbolic links on Windows with .NET by using the CreateSymbolicLink method of the Win32 class in the Microsoft.Win32.SafeHandles namespace.
1using System;
2using Microsoft.Win32.SafeHandles;
3using System.Runtime.InteropServices;
4
5class Program
6{
7 [DllImport("kernel32.dll", SetLastError = true)]
8 static extern bool CreateSymbolicLink(
9 string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);
10
11 enum SymbolicLink
12 {
13 File = 0,
14 Directory = 1
15 }
16
17 static void Main(string[] args)
18 {
19 string linkPath = @"C:\link";
20 string targetPath = @"C:\target";
21
22 bool linkCreated = CreateSymbolicLink(linkPath, targetPath, SymbolicLink.Directory);
23
24 if (linkCreated)
25 {
26 Console.WriteLine("CreateSymbolicLink succeeded.");
27 }
28 else
29 {
30 int errorCode = Marshal.GetLastWin32Error();
31 Console.WriteLine("CreateSymbolicLink failed with error code: " + errorCode);
32 }
33 }
34}
To map an error code (int) to a useful message, you have to look into the Windows Error Codes table. Windows System Error Codes
Related articles

Mar 25, 2026 · 14 min read
The new Microsoft Testing Platform for .NET: An introduction with practical samples and migration guidance
Testing in .NET has historically been associated with VSTest. That choice was reasonable for a long time because VSTest offered broad …

Mar 17, 2026 · 15 min read
GitHub Copilot - Custom Agents for Full-Stack Teams: A Practical Operating Model for .NET, React and Azure
GitHub Copilot custom agents allow teams to define specialized AI assistants, each with its own role, tool access and behavioral boundaries. …

Mar 10, 2026 · 15 min read
.NET NuGet Trusted Publishing with GitHub Actions
Publishing NuGet packages has traditionally required one uncomfortable compromise: a long-lived API key had to exist somewhere in the …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

Comments