Tuesday, October 24, 2006 6:34 PM bart

Windows Vista - Creating symbolic links with C#

Windows Vista has support for symbolic links, and of course the API does too. Support for hard links has been there since the Windows 2000 days and won't be covered here.

Here's a piece of sample code:

using System;
using
System.Runtime.InteropServices;
using
System.IO;

namespace
mklink
{
   class
Program
   {
      [
DllImport("kernel32.dll"
)]
      static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int
dwFlags);

      static int SYMLINK_FLAG_DIRECTORY = 1;

      static void Main(string
[] args)
      {
         //
         // Symbolic file link bar.txt <<===>> foo.txt
         //
         string symF = "bar.txt"
;
         string targetF = "foo.txt"
;

         Console.WriteLine(">echo \"Hello World\" > {0}"
, targetF);
         StreamWriter sw = File
.CreateText(targetF);
         sw.WriteLine(
"Hello World"
);
         sw.Close();
         Console
.WriteLine();

         Console.WriteLine(">mklink {0} {1}"
, symF, targetF);
         if
(CreateSymbolicLink(symF, targetF, 0))
            Console.WriteLine("symbolic link created for {0} <<===>> {1}"
, symF, targetF);
         Console
.WriteLine();

         Console.WriteLine(">type {0}"
, targetF);
         Console.WriteLine(File
.ReadAllText(targetF));
         Console
.WriteLine();

         //
         // Symbolic directory link bar <<===>> foo
         //
         string symD = "bar"
;
         string targetD = "foo"
;

         Console.WriteLine(">mkdir {0}"
, targetD);
         Directory
.CreateDirectory(targetD);
         Console
.WriteLine();

         Console.WriteLine(">echo \"Hello World\" > {0}\\demo.txt"
, targetD);
         StreamWriter sw2 = File.CreateText(targetD + \\demo.txt
);
         sw2.WriteLine(
"Hello World"
);
         sw2.Close();
         Console
.WriteLine();

         Console.WriteLine(">mklink /d {0} {1}"
, symD, targetD);
         if
(CreateSymbolicLink(symD, targetD, SYMLINK_FLAG_DIRECTORY))
            Console.WriteLine("symbolic link created for {0} <<===>> {1}"
, symD, targetD);
         Console
.WriteLine();

         Console
.WriteLine(">dir {0}", targetD);
         foreach (string f in Directory
.GetFiles(targetD))
            Console
.WriteLine(f);
      }
   }
}

System;
using
System.Runtime.InteropServices;
using
System.IO;

namespace
mklink
{
   class
Program
   {
      [
DllImport("kernel32.dll"
)]
      static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int
dwFlags);

      static int SYMLINK_FLAG_DIRECTORY = 1;

      static void Main(string
[] args)
      {
         //
         // Symbolic file link bar.txt <<===>> foo.txt
         //
         string symF = "bar.txt"
;
         string targetF = "foo.txt"
;

         Console.WriteLine(">echo \"Hello World\" > {0}"
, targetF);
         StreamWriter sw = File
.CreateText(targetF);
         sw.WriteLine(
"Hello World"
);
         sw.Close();
         Console
.WriteLine();

         Console.WriteLine(">mklink {0} {1}"
, symF, targetF);
         if
(CreateSymbolicLink(symF, targetF, 0))
            Console.WriteLine("symbolic link created for {0} <<===>> {1}"
, symF, targetF);
         Console
.WriteLine();

         Console.WriteLine(">type {0}"
, targetF);
         Console.WriteLine(File
.ReadAllText(targetF));
         Console
.WriteLine();

         //
         // Symbolic directory link bar <<===>> foo
         //
         string symD = "bar"
;
         string targetD = "foo"
;

         Console.WriteLine(">mkdir {0}"
, targetD);
         Directory
.CreateDirectory(targetD);
         Console
.WriteLine();

         Console.WriteLine(">echo \"Hello World\" > {0}\\demo.txt"
, targetD);
         StreamWriter sw2 = File.CreateText(targetD + \\demo.txt
);
         sw2.WriteLine(
"Hello World"
);
         sw2.Close();
         Console
.WriteLine();

         Console.WriteLine(">mklink /d {0} {1}"
, symD, targetD);
         if
(CreateSymbolicLink(symD, targetD, SYMLINK_FLAG_DIRECTORY))
            Console.WriteLine("symbolic link created for {0} <<===>> {1}"
, symD, targetD);
         Console
.WriteLine();

         Console
.WriteLine(">dir {0}", targetD);
         foreach (string f in Directory
.GetFiles(targetD))
            Console
.WriteLine(f);
      }
   }
}

The code is pretty self-explanatory. Compile it and execute it in an empty folder. The following directory structure will be created:

C:.
│   foo.txt

└───bar
        demo.txt

Next, symbolic links are created, yielding the following result:

C:.
│   bar.txt
│   foo.txt

├───bar
│       demo.txt

└───foo
        demo.txt

Indicated in red are the symbolic links: one for a file and another one for a directory. Below, the dir output and the Windows Explorer view are displayed:

Check out the mklink command in Vista too:

Enjoy!

kick it on DotNetKicks.com

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

No Comments