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"); }
source share