Example #1
0
        /// <summary>
        /// Retrieves a stream for writing from a file in the specified parent folder.
        /// </summary>
        /// <param name="rootDirectory">The Windows Runtime IStorageFolder object that contains the file to write to.</param>
        /// <param name="relativePath">The path, relative to the root folder, to the file to write to.</param>
        /// <param name="creationCollisionOption"></param>
        /// <returns></returns>
        public static Task <Stream> OpenStreamForWriteAsync(this IStorageFolder rootDirectory, string relativePath, CreationCollisionOption creationCollisionOption)
        {
#if WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE
            return(WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync((global::Windows.Storage.StorageFolder)((StorageFolder)rootDirectory), relativePath, (global::Windows.Storage.CreationCollisionOption)((int)creationCollisionOption)));
#elif __ANDROID__ || __UNIFIED__ || WIN32
            string newPath = Path.Combine(rootDirectory.Path, relativePath);
            return(Task.FromResult <Stream>(global::System.IO.File.OpenWrite(newPath)));
#else
            throw new PlatformNotSupportedException();
#endif
        }
Example #2
0
        /// <summary>
        /// Retrieves a stream for writing to a specified file.
        /// </summary>
        /// <param name="windowsRuntimeFile">The Windows Runtime IStorageFile object to write to.</param>
        /// <returns></returns>
        public static Task <Stream> OpenStreamForWriteAsync(this IStorageFile windowsRuntimeFile)
        {
#if WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE
            return(WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync((global::Windows.Storage.StorageFile)((StorageFile)windowsRuntimeFile)));
#elif __ANDROID__ || __UNIFIED__ || WIN32
            if (windowsRuntimeFile == null)
            {
                throw new ArgumentNullException("windowsRuntimeFile");
            }

#if __ANDROID__
            if (windowsRuntimeFile.Path.StartsWith("content:"))
            {
                return(Task.FromResult <Stream>(Android.App.Application.Context.ContentResolver.OpenInputStream(Android.Net.Uri.Parse(windowsRuntimeFile.Path))));
            }
#endif

            return(Task.FromResult <Stream>(global::System.IO.File.OpenWrite(windowsRuntimeFile.Path)));
#else
            throw new PlatformNotSupportedException();
#endif
        }