File access sample

This sample shows how to create, read, write, copy and delete a file, how to retrieve file properties, and how to track a file or folder so that your app can access it again. This sample uses Windows.Storage and Windows.Storage.AccessCache API.

 
 
 
 
 
(32)
99 397 раз
Добавить в избранное
09.04.2013
E-mail del.icio.us Digg Facebook Reddit Slashdot Twitter Windows Live Favorites Windows Live Spaces

Обозреватель решений

C++
C#
JavaScript
VB.NET
'*********************************************************
'
' Copyright (c) Microsoft. All rights reserved.
' THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
' IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
' PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
'
'*********************************************************

Imports SDKTemplate

Imports System
Imports System.IO
Imports Windows.Storage
Imports Windows.Storage.Streams
Imports Windows.UI.Xaml
Imports Windows.UI.Xaml.Controls
Imports Windows.UI.Xaml.Navigation

''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Partial Public NotInheritable Class Scenario4
    Inherits SDKTemplate.Common.LayoutAwarePage
    Private rootPage As MainPage = MainPage.Current

    Public Sub New()
        Me.InitializeComponent()
        AddHandler WriteToStreamButton.Click, AddressOf WriteToStreamButton_Click
        AddHandler ReadFromStreamButton.Click, AddressOf ReadFromStreamButton_Click
    End Sub

    Private Async Sub WriteToStreamButton_Click(sender As Object, e As RoutedEventArgs)
        Try
            rootPage.ResetScenarioOutput(OutputTextBlock)
            Dim file As StorageFile = rootPage.sampleFile
            If file IsNot Nothing Then
                Dim userContent As String = InputTextBox.Text
                If Not String.IsNullOrEmpty(userContent) Then
                    Using transaction = CType(Await file.OpenTransactedWriteAsync, StorageStreamTransaction)
                        Using dataWriter As New DataWriter(transaction.Stream)
                            dataWriter.WriteString(userContent)
                            transaction.Stream.Size = Await dataWriter.StoreAsync() ' reset stream size to override the file
                            Await transaction.CommitAsync()
                            OutputTextBlock.Text = "The following text was written to '" & file.Name & "' using a stream:" & Environment.NewLine & Environment.NewLine & userContent
                        End Using
                    End Using
                Else
                    OutputTextBlock.Text = "The text box is empty, please write something and then click 'Write' again."
                End If
            End If
        Catch ex As FileNotFoundException
            rootPage.NotifyUserFileNotExist()
        End Try
    End Sub

    Private Async Sub ReadFromStreamButton_Click(sender As Object, e As RoutedEventArgs)
        Try
            rootPage.ResetScenarioOutput(OutputTextBlock)
            Dim file As StorageFile = rootPage.sampleFile
            If file IsNot Nothing Then
                Using readStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
                    Using dataReader As New DataReader(readStream)
                        Dim size As UInt64 = readStream.Size
                        If size <= UInt32.MaxValue Then
                            Dim numBytesLoaded As UInt32 = Await dataReader.LoadAsync(CType(size, UInt32))
                            Dim fileContent As String = dataReader.ReadString(numBytesLoaded)
                            OutputTextBlock.Text = "The following text was read from '" & file.Name & "' using a stream:" & Environment.NewLine & Environment.NewLine & fileContent
                        Else
                            OutputTextBlock.Text = "File " & file.Name & " is too big for LoadAsync to load in a single chunk. Files larger than 4GB need to be broken into multiple chunks to be loaded by LoadAsync."
                        End If
                    End Using
                End Using
            End If
        Catch ex As FileNotFoundException
            rootPage.NotifyUserFileNotExist()
        End Try
    End Sub

End Class