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 382krát
Přidat k oblíbeným
9.4.2013
E-mail Twitter del.icio.us Digg Facebook

Průzkumník řešení

C++
C#
JavaScript
VB.NET
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved

(function () {
    "use strict";

    var page = WinJS.UI.Pages.define("/html/scenario2.html", {
        ready: function (element, options) {
            document.getElementById("writeText").addEventListener("click", writeText, false);
            document.getElementById("readText").addEventListener("click", readText, false);
            // To test if "sample.dat" is created.
            SdkSample.validateFileExistence();
        }
    });

    // Writes some text to 'sample.dat'
    function writeText() {
        if (SdkSample.sampleFile !== null) {
            var textArea = document.getElementById("textarea");
            var userContent = textArea.innerText;
            var outputDiv = document.getElementById("output");
            if (userContent !== "") {
                Windows.Storage.FileIO.writeTextAsync(SdkSample.sampleFile, userContent).done(function () {
                    outputDiv.innerHTML = "The following text was written to '" + SdkSample.sampleFile.name + "':<br /><br />" + userContent;
                },
                function (error) {
                    WinJS.log && WinJS.log(error, "sample", "error");
                });
            } else {
                outputDiv.innerHTML = "The text box is empty, please write something and then click 'Write' again.";
            }
        }
    }

    // Reads text from 'sample.dat'
    function readText() {
        if (SdkSample.sampleFile !== null) {
            Windows.Storage.FileIO.readTextAsync(SdkSample.sampleFile).done(function (fileContent) {
                var outputDiv = document.getElementById("output");
                outputDiv.innerHTML = "The following text was read from '" + SdkSample.sampleFile.name + "':<br /><br />" + fileContent;
            },
            function (error) {
                WinJS.log && WinJS.log(error, "sample", "error");
            });
        }
    }
})();