Check if the file is real or a symlink

Is there a way to tell using C # if the file is a real or symbolic link?

I have broken through the MSDN W32 docs ( http://msdn.microsoft.com/en-us/library/aa364232(VS.85).aspx ) and cannot find anything to verify this. I am using CreateSymbolicLink from here and it works great.

+24
source share
5 answers

I have the source code for symlinks posted on my blog that will allow you to:

  • create symbolic links
  • check if the path is a symbolic link
  • get the target of a symbolic link

It also contains NUnit test cases that you can extend.

Soft bit:

private static SafeFileHandle getFileHandle(string path) { return CreateFile(path, genericReadAccess, shareModeAll, IntPtr.Zero, openExisting, fileFlagsForOpenReparsePointAndBackupSemantics, IntPtr.Zero); } public static string GetTarget(string path) { SymbolicLinkReparseData reparseDataBuffer; using (SafeFileHandle fileHandle = getFileHandle(path)) { if (fileHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } int outBufferSize = Marshal.SizeOf(typeof(SymbolicLinkReparseData)); IntPtr outBuffer = IntPtr.Zero; try { outBuffer = Marshal.AllocHGlobal(outBufferSize); int bytesReturned; bool success = DeviceIoControl( fileHandle.DangerousGetHandle(), ioctlCommandGetReparsePoint, IntPtr.Zero, 0, outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero); fileHandle.Close(); if (!success) { if (((uint)Marshal.GetHRForLastWin32Error()) == pathNotAReparsePointError) { return null; } Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } reparseDataBuffer = (SymbolicLinkReparseData)Marshal.PtrToStructure( outBuffer, typeof(SymbolicLinkReparseData)); } finally { Marshal.FreeHGlobal(outBuffer); } } if (reparseDataBuffer.ReparseTag != symLinkTag) { return null; } string target = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer, reparseDataBuffer.PrintNameOffset, reparseDataBuffer.PrintNameLength); return target; } 

I.e:

+18
source
 private bool IsSymbolic(string path) { FileInfo pathInfo = new FileInfo(path); return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint); } 
+22
source

Here is an example of distinguishing files and directories from file links and directory links.

Links to files or directories retain their own attributes (creation date, permission) separately from their goals.

Links to files can be deleted (for example, using "del") without affecting the target file.

Directory links can be removed (for example, "rmdir") without affecting the target directory. Be careful when using "rd / s". This will remove the link to the directory link.

The FileAttributes key for checking both FileInfo and DirectoryInfo is FileAttributes.ReparsePoint .

 static void Main( string[] args ) { FileInfo file_info = new FileInfo(args[0]); DirectoryInfo directory_info = new DirectoryInfo(args[0]); bool is_file = file_info.Exists; bool is_directory = directory_info.Exists; if (is_file) { Console.WriteLine(file_info.ToString() + " is a file"); if ( file_info.Attributes.HasFlag(FileAttributes.ReparsePoint) ) Console.WriteLine(args[0] + " is a Windows file link"); } else if (is_directory) { Console.WriteLine(directory_info.ToString() + " is a directory"); if ( directory_info.Attributes.HasFlag(FileAttributes.ReparsePoint) ) Console.WriteLine(args[0] + " is a Windows directory link"); } 
+7
source

According to this answer to the stack overflow question Find out if the file is a symbolic link in PowerShell, getting System.IO.FileAttributes for the file (via File.GetAttributes ) and testing the ReparsePoint bit works, If the bit is set, this is a symbolic link or connection point. If not, this is a regular file (or hard link).

+1
source

GetFileInformationByHandle fills the BY_HANDLE_FILE_INFORMATION structure, which has a dwFileAttributes field, where bits are set with information about file attributes (details here ). In particular, look at the bit in the mask ...:

FILE_ATTRIBUTE_REPARSE_POINT 1024 0x0400

A file or directory that has an associated reprocessing point, or the file is a symbolic link.

0
source

Source: https://habr.com/ru/post/1411056/


All Articles