コード例 #1
0
        public static async Task<Stream> OpenWriteStream(string filePath, bool overwrite = true, ApplicationDataLocation location = ApplicationDataLocation.Local)
        {
            StorageFolder folder = GetApplicationDataFolder(location);
            folder = await folder.CreateFolderLazy(filePath);
            var fileName = Path.GetFileName(filePath);
            CreationCollisionOption createOption = (overwrite ? CreationCollisionOption.ReplaceExisting : CreationCollisionOption.OpenIfExists);

            try
            {
                var file = await folder.CreateFileAsync(fileName, createOption);
                if (file == null)
                {
                    return null;
                }

                return await file.OpenStreamForWriteAsync();
            }
            catch (IOException)
            {
                return null;
            }
            catch (UnauthorizedAccessException)
            {
                return null;
            }
        }
コード例 #2
0
        public static async Task <StorageFile> OpenFile(string filePath, bool overwrite = true, ApplicationDataLocation location = ApplicationDataLocation.Local)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            StorageFolder folder = GetApplicationDataFolder(location);

            folder = await folder.CreateFolderLazy(filePath);

            var fileName = Path.GetFileName(filePath);
            CreationCollisionOption createOption = (overwrite ? CreationCollisionOption.ReplaceExisting : CreationCollisionOption.OpenIfExists);

            try
            {
                return(await folder.CreateFileAsync(fileName, createOption));
            }
            catch (IOException)
            {
                return(null);
            }
            catch (UnauthorizedAccessException)
            {
                return(null);
            }
        }