Detecting symbolic links and pipes in Mono

2019-05-15 01:16发布

Is there a way to distinguish special files like symbolic links and pipes using C# and Mono?

The application is a multi-platform backup tool, so I want to avoid using interop libraries or 'C' dll's and look for a straight managed code solution.

1条回答
再贱就再见
2楼-- · 2019-05-15 01:48

After digging around some more, I've found a solution.

Adding a reference to Mono.Posix to a project gives access to some of the Unix file system attributes.

Mono.Unix.UnixSymbolicLinkInfo i = new Mono.Unix.UnixSymbolicLinkInfo( path );
switch( i.FileType )
{
   case FileTypes.SymbolicLink:
   case FileTypes.Fifo:
   case FileTypes.Socket:
   case FileTypes.BlockDevice:
   case FileTypes.CharacterDevice:
   case FileTypes.Directory:
   case FileTypes.RegularFile:
}

The above code helps identify a range of special files.

Using UnixSymbolicLinkInfo is important because both UnixFileInfo and UnixDirectoryInfo resolve the symbolic link prior to testing.

查看更多
登录 后发表回答