public void CreateFileHarddiskVolume() { using (var file = FileMethods.CreateFile(@"\\?\GLOBALROOT\Device\HarddiskVolume1", CreationDisposition.OpenExisting, DesiredAccess.GenericRead)) { file.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(file).Should().Be(FileType.Disk); } }
public void CreateFileComPort() { using (var file = FileMethods.CreateFile(@"COM4", CreationDisposition.OpenExisting, DesiredAccess.GenericRead)) { file.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(file).Should().Be(FileType.Character); } }
public void CreateFileHarddiskVolume() { using (var file = FileMethods.CreateFile(@"\\?\GLOBALROOT\Device\HarddiskVolume1", DesiredAccess.GENERIC_READ, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING)) { file.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(file).Should().Be(FileType.FILE_TYPE_DISK); } }
public void GetFileTypeDisk() { string tempPath = FileMethods.GetTempPath(); using (var directory = DirectoryMethods.CreateDirectoryHandle(tempPath)) { FileMethods.GetFileType(directory).Should().Be(FileType.Disk); } }
public void CreateFileRawParition() { // You can't open with read/write access unless running elevated using (var file = FileMethods.CreateFile(@"\\?\GLOBALROOT\Device\Harddisk0\Partition0", CreationDisposition.OpenExisting, 0)) { file.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(file).Should().Be(FileType.Disk); } }
public void CreateFileRawParition() { // You can't open with read/write access unless running elevated using (var file = FileMethods.CreateFile(@"\\?\GLOBALROOT\Device\Harddisk0\Partition0", DesiredAccess.NONE, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING)) { file.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(file).Should().Be(FileType.FILE_TYPE_DISK); } }
public void FileTypeOfFile() { using (var cleaner = new TestFileCleaner()) { using (var testFile = FileMethods.CreateFile(cleaner.GetTestPath(), CreationDisposition.CreateNew)) { FileMethods.GetFileType(testFile).Should().Be(FileType.Disk); } } }
// Create a symbolic link at "newpath" that points to "oldpath". // Throws "NotSupportedException" if the filesystem does not // support the creation of symbolic links or ".lnk" files. public static void CreateLink(String oldpath, String newpath) { // Validate and normalize the parameters. Directory.ValidatePath(oldpath); Directory.ValidatePath(newpath); oldpath = Path.NormalizeSeparators(oldpath); newpath = Path.NormalizeSeparators(newpath); // Try to create the symlink using the engine. Errno error = FileMethods.CreateLink(oldpath, newpath); if (error == Errno.Success) { return; } else if (error != Errno.EPERM) { Directory.HandleErrorsFile(error); } // Bail out if we are not on Windows. if (!IsWindows()) { throw new NotSupportedException(_("IO_NotSupp_Symlinks")); } // Make sure that we have ".lnk" on the end of the pathname. if (!EndsInLnk(newpath)) { if (FileMethods.GetFileType(newpath) != FileType.unknown) { // There already exists something at this location. Directory.HandleErrorsFile(Errno.EEXIST); } newpath += ".lnk"; } if (FileMethods.GetFileType(newpath) != FileType.unknown) { // There already exists something at this location. Directory.HandleErrorsFile(Errno.EEXIST); } // Create the shortcut information to place in the file. ShortcutInformation info = new ShortcutInformation(); info.Description = oldpath; info.RelativePath = oldpath; info.ShowWindow = ShowWindow.Normal; // Write the shortcut information to the file. WriteShortcut(newpath, info, false); }
// Determine if a directory with a specific path exists. public static bool Exists(String path) { try { ValidatePath(path); } catch (ArgumentException) { return(false); } return(FileMethods.GetFileType(path) == FileType.directory); }
// Determine whether a file exists. public static bool Exists(String path) { try { Directory.ValidatePath(path); } catch (Exception) { return(false); } FileType type = FileMethods.GetFileType(path); return(type != FileType.directory && type != FileType.unknown); }
public void BasicCreateMailslot() { const string mailslotName = @"\\.\mailslot\basiccreatemailslottest"; using (var handle = MailslotMethods.CreateMailslot(mailslotName)) { handle.IsInvalid.Should().BeFalse(); using (var fileHandle = FileMethods.CreateFile(mailslotName, CreationDisposition.OpenExisting, 0)) { fileHandle.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(fileHandle).Should().Be(FileType.Unknown); var modeInfo = FileMethods.GetFileMode(fileHandle); // The mailslot was opened synchronously modeInfo.Should().Be(FileAccessModes.SynchronousNotAlertable); } } }
public void BasicCreateMailslot() { const string mailslotName = @"\\.\mailslot\basiccreatemailslottest"; using (var handle = MailslotDesktopMethods.CreateMailslot(mailslotName)) { handle.IsInvalid.Should().BeFalse(); using (var fileHandle = FileMethods.CreateFile(mailslotName, DesiredAccess.NONE, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING)) { fileHandle.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(fileHandle).Should().Be(FileType.FILE_TYPE_UNKNOWN); var modeInfo = FileDesktopMethods.GetFileMode(fileHandle); // The mailslot was opened synchronously modeInfo.Should().Be(FILE_MODE_INFORMATION.FILE_SYNCHRONOUS_IO_NONALERT); } } }
public void CreateMailslotAsync() { const string mailslotName = @"\\.\mailslot\asynccreatemailslottest"; using (var handle = MailslotMethods.CreateMailslot(mailslotName)) { handle.IsInvalid.Should().BeFalse(); using (var fileHandle = FileMethods.CreateFile(mailslotName, CreationDisposition.OpenExisting, 0, ShareModes.ReadWrite, FileAttributes.None, FileFlags.Overlapped)) { fileHandle.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(fileHandle).Should().Be(FileType.Unknown); var modeInfo = FileMethods.GetFileMode(fileHandle); // The mailslot was opened asynchronously (e.g. no synchronous flag) modeInfo.Should().Be((FileAccessModes)0); } } }
public void CreateMailslotAsync() { const string mailslotName = @"\\.\mailslot\asynccreatemailslottest"; using (var handle = MailslotDesktopMethods.CreateMailslot(mailslotName)) { handle.IsInvalid.Should().BeFalse(); using (var fileHandle = FileMethods.CreateFile(mailslotName, DesiredAccess.NONE, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING, FileAttributes.NONE, FileFlags.FILE_FLAG_OVERLAPPED)) { fileHandle.IsInvalid.Should().BeFalse(); FileMethods.GetFileType(fileHandle).Should().Be(FileType.FILE_TYPE_UNKNOWN); var modeInfo = FileDesktopMethods.GetFileMode(fileHandle); // The mailslot was opened asynchronously (e.g. no synchronous flag) modeInfo.Should().Be((FILE_MODE_INFORMATION)0); } } }
// Determine if a pathname is a symbolic link. public static bool IsSymbolicLink(String pathname) { // Validate the parameter and normalize it. if (pathname == null) { throw new ArgumentNullException("pathname"); } pathname = Path.NormalizeSeparators(pathname); // If we are on Windows, then check for a ".lnk" file. if (IsWindows()) { if (!EndsInLnk(pathname)) { // We need to add ".lnk" to the pathname first. if (FileMethods.GetFileType(pathname) != FileType.unknown) { // The path exists, so it cannot be a symlink. return(false); } pathname += ".lnk"; } if (FileMethods.GetFileType(pathname) != FileType.regularFile) { // The path does not exist or is not a regular file. return(false); } return(true); } // Get the file type from the engine and check it. FileType type = FileMethods.GetFileType(pathname); return(type == FileType.symbolicLink); }
// Read the contents of a symbolic link. Returns null if not a // symbolic link, or throws an exception if the pathname is invalid. public static String ReadLink(String pathname) { // Validate the parameter and normalize it. Directory.ValidatePath(pathname); pathname = Path.NormalizeSeparators(pathname); // Read the contents of the symlink using the engine. String contents; Errno error = FileMethods.ReadLink(pathname, out contents); if (error != Errno.Success) { Directory.HandleErrorsFile(error); } if (contents != null) { return(contents); } // Bail out if we aren't on Windows. if (!IsWindows()) { return(null); } // Make sure that we have ".lnk" on the end of the name. if (!EndsInLnk(pathname)) { if (FileMethods.GetFileType(pathname) != FileType.unknown) { // Something exists here and it does not have a ".lnk" // extension, so it cannot possibly be a symlink. return(null); } pathname += ".lnk"; } FileType type = FileMethods.GetFileType(pathname); if (type == FileType.unknown) { // The pathname does not exist. Directory.HandleErrorsFile(Errno.ENOENT); } else if (type != FileType.regularFile) { // The pathname exists but it is not a regular file, // so it cannot possibly be a symbolic link. return(null); } // Read the contents of the Windows shortcut file. ShortcutInformation info = ReadShortcut(pathname); if (info == null) { return(null); } else { return(info.RelativePath); } }