コード例 #1
0
 public static void SetLastAccessTime(string fullPath, DateTimeOffset time, bool asDirectory, bool backupMode = false)
 {
     using (SafeFileHandle handle = OpenHandle(fullPath, asDirectory, true, backupMode))
     {
         if (!Win32Api.Kernel32.SetFileTime(handle, lastAccessTime: time.ToFileTime()))
         {
             throw PalWin32FileStream.GetExceptionForLastWin32Error(fullPath);
         }
     }
 }
コード例 #2
0
 public static void SetAttributes(string fullPath, FileAttributes attributes)
 {
     if (!Win32Api.Kernel32.SetFileAttributes(fullPath, (int)attributes))
     {
         int errorCode = Marshal.GetLastWin32Error();
         if (errorCode == Win32Api.Errors.ERROR_INVALID_PARAMETER)
         {
             throw new ArgumentException(nameof(attributes));
         }
         throw PalWin32FileStream.GetExceptionForWin32Error(errorCode, fullPath);
     }
 }
コード例 #3
0
        public static SafeFileHandle OpenHandle(string fullPath, bool asDirectory, bool writeMode = false, bool backupMode = false, bool asyncMode = true, int additionalFlags = 0)
        {
            string root = fullPath.Substring(0, Win32PathInternal.GetRootLength(fullPath.AsSpan()));

            if (root == fullPath && root[1] == Path.VolumeSeparatorChar)
            {
                // intentionally not fullpath, most upstack public APIs expose this as path.
                throw new ArgumentException("path");
            }

            if (asyncMode)
            {
                additionalFlags |= (int)FileOptions.Asynchronous;
            }

            using (Lfs.EnterDisableMediaInsertionPrompt())
            {
                SafeFileHandle handle = Win32Api.Kernel32.CreateFile(
                    fullPath,
                    writeMode ? Win32Api.Kernel32.GenericOperations.GENERIC_WRITE | Win32Api.Kernel32.GenericOperations.GENERIC_READ : Win32Api.Kernel32.GenericOperations.GENERIC_READ,
                    FileShare.ReadWrite | FileShare.Delete,
                    FileMode.Open,
                    ((asDirectory || backupMode) ? Win32Api.Kernel32.FileOperations.FILE_FLAG_BACKUP_SEMANTICS : 0) | additionalFlags);

                if (handle.IsInvalid)
                {
                    int errorCode = Marshal.GetLastWin32Error();

                    // NT5 oddity - when trying to open "C:\" as a File,
                    // we usually get ERROR_PATH_NOT_FOUND from the OS.  We should
                    // probably be consistent w/ every other directory.
                    if (!asDirectory && errorCode == Win32Api.Errors.ERROR_PATH_NOT_FOUND && fullPath.Equals(Directory.GetDirectoryRoot(fullPath)))
                    {
                        errorCode = Win32Api.Errors.ERROR_ACCESS_DENIED;
                    }

                    throw PalWin32FileStream.GetExceptionForWin32Error(errorCode, fullPath);
                }

                if (((FileOptions)additionalFlags).Bit(FileOptions.Asynchronous))
                {
                    handle._SetAsync(true);
                    ThreadPool.BindHandle(handle);
                }
                else
                {
                    handle._SetAsync(false);
                }

                return(handle);
            }
        }
コード例 #4
0
        protected override async Task <IEnumerable <RawDiskItemData> > RescanRawDisksImplAsync(CancellationToken cancel = default)
        {
            List <RawDiskItemData> ret = new List <RawDiskItemData>();

            if (Env.IsWindows)
            {
                if (Env.IsAdmin == false)
                {
                    throw new CoresException("Administrator privilege is required.");
                }

                for (int i = 0; i < 100; i++)
                {
                    string name    = "PhysicalDrive" + i.ToString();
                    string rawPath = @"\\.\" + name;

                    try
                    {
                        using (var handle = PalWin32FileStream.CreateFileOpenHandle(FileMode.Open, FileShare.ReadWrite, FileOptions.None, FileAccess.Read, rawPath))
                        {
                            try
                            {
                                var geometry = await Win32ApiUtil.DiskGetDriveGeometryAsync(handle, rawPath, cancel);

                                RawDiskItemType type = RawDiskItemType.Unknown;
                                if (geometry.MediaType == Win32Api.Kernel32.NativeDiskType.FixedMedia)
                                {
                                    type = RawDiskItemType.FixedMedia;
                                }
                                else if (geometry.MediaType == Win32Api.Kernel32.NativeDiskType.RemovableMedia)
                                {
                                    type = RawDiskItemType.RemovableMedia;
                                }

                                ret.Add(new RawDiskItemData(name, rawPath, type, geometry.DiskSize));
                            }
                            catch
                            { }
                        }
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                throw new PlatformNotSupportedException();
            }

            return(ret);
        }
コード例 #5
0
        public static FileAttributes GetAttributes(string fullPath, bool backupMode = false)
        {
            int flags = backupMode ? Win32Api.Kernel32.FileOperations.FILE_FLAG_BACKUP_SEMANTICS : 0;

            Win32Api.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Api.Kernel32.WIN32_FILE_ATTRIBUTE_DATA();
            int errorCode = FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: true);

            if (errorCode != 0)
            {
                throw PalWin32FileStream.GetExceptionForWin32Error(errorCode, fullPath);
            }

            return((FileAttributes)data.dwFileAttributes);
        }
コード例 #6
0
 public static Exception GetWin32ErrorException(int?errorCode = null, string?argument = null)
 => PalWin32FileStream.GetExceptionForWin32Error(errorCode ?? Marshal.GetLastWin32Error(), argument);