public static SafeFileHandle CreateSafeFileHandle(
this IStorageFolder rootDirectory,
string relativePath,
FileMode mode,
FileAccess access,
FileShare share = FileShare.Read,
FileOptions options = FileOptions.None)
{
if (rootDirectory == null)
{
throw new ArgumentNullException(nameof(rootDirectory));
}
if (relativePath == null)
{
throw new ArgumentNullException(nameof(relativePath));
}
HANDLE_CREATION_OPTIONS creationOptions = FileModeToCreationOptions(mode);
HANDLE_ACCESS_OPTIONS accessOptions = FileAccessToHandleAccessOptions(access);
HANDLE_SHARING_OPTIONS sharingOptions = FileShareToHandleSharingOptions(share);
HANDLE_OPTIONS handleOptions = FileOptionsToHandleOptions(options);
IStorageFolderHandleAccess handleAccess = ((object)rootDirectory) as IStorageFolderHandleAccess;
if (handleAccess == null)
{
return(null);
}
SafeFileHandle handle;
int result = handleAccess.Create(
relativePath,
creationOptions,
accessOptions,
sharingOptions,
handleOptions,
IntPtr.Zero,
out handle);
if (result != 0)
{
throw Win32Marshal.GetExceptionForWin32Error(Win32Marshal.TryMakeWin32ErrorCodeFromHR(result), relativePath);
}
return(handle);
}