[Mono-list] Hard linking

Jonathan Pryor jonpryor at vt.edu
Tue Sep 2 13:19:48 EDT 2008


On Tue, 2008-09-02 at 07:30 -0700, Magikat wrote:
> Could anyone tell me if hard-linking on both Linux and Windows is supported
> under the .Net Framework.

As far as I'm aware, symbolic links and hard links are NOT supported as
distinct entities under .NET.  That is, you can read a hard link or
symbolic link normally, but there is no way to determine that the file
is a hard link or symbolic link from within .NET.

> If not, could you perhaps point to some information that might help writing
> something to help with hard-linking in C#.

On Mono/Linux, you can use the various Mono.Unix classes in
Mono.Posix.dll:

	UnixFileSystemInfo e = 
		UnixFileSystemInfo.GetFileSystemEntry(file);
	if (e.IsSymbolicLink)
		// file is a symbolic link
	else if (e.IsRegularFile && e.LinkCount > 1)
		// file is a hard link
	...

Or you can use Mono.Unix.Native.Syscall for the ~actual POSIX APIs.

This doesn't work for Win32, though, so for Win32 support you'd need to
P/Invoke to the corresponding Win32 APIs, such as CreateHardLink() [1]
and CreateSymbolicLink() [2].

 - Jon

[0] http://www.go-mono.com/docs/index.aspx?tlink=0@ecma%3a105%23UnixFileSystemInfo%2f
[1] http://msdn.microsoft.com/en-us/library/aa363860(VS.85).aspx
[2] http://msdn.microsoft.com/en-us/library/aa363866(VS.85).aspx




More information about the Mono-list mailing list