Partager via


Windows 8 Dev: Create, Read, Write, Copy, and Delete

Requirements: Windows 8 and Visual Studio 2012

Sample 1: Access a File

Visual Studio 2012 comes with several different online templates you can download online. You simply access the templates by :

  1. Starting Visual Studio 2012
  2. Click New Project
  3. Click Online in the left hand menu
  4. Click samples
  5. Select from the choices of JavaScript, Visual: basic, C#,  C++, and Visual F#

(sav)Untitled2 (3)

For these tutorials on getting started with Windows Store Apps I am going to be focusing on HTML 5 and JavaScript.  After you have created your first  Hello World  program your next step is to begin understanding how to create , read , write , copy , and delete information to your app.  File access sample on the Dev Center windows website is a great start on how to do this; don’t worry C++, C#, JavaScript , and VB.NET  developer you were not forgotten click on tabs at  the top of this page to get your tutorial.

 

What you will learn

This sample will enable the you to:

1.Create a file in Doc Library

2.RW text to a file

3.Write and read bytes in a file

4.RW a file using a stream

5.Displays file properties

6.Persisting Access

7.Copy a file

8.Delete a file

Sample Description 

APIs Used:  Windows.Storage and Windows.Storage.AccessCache.

This sample demonstrates to do steps 1 through 8 to retrieve, track  a file; using the Windows. Storage and Windows.Storage.AccessCache.          

The sample has been split into 8 HTML and JS  files labeled  Scenario1 to 8. In this post I will  re-using the code used here showing a step by step process on how to use this code on your own.

 

Lesson 1: Create a File

Start   Visual Studio 2012 select a blank template, name the app  Create Read Write Delete and click create. Add  new folder to your solution by right clicking on your Solution name and select add new folder rename HTML.

Goals: create a generic data file(.dat) in your document library. To complete  this demo  download the samples and walkthrough the steps.

For this section of  the demo you will need to open the Scenario1. HTML and Scenario1.JS in the project solution.

Scenario1.HTML

In the  Scenario1  HTML  the  data-win-control in the <div>  is going to jump out to you. The data-win-control enables a div element host the specified window library for a java script control like Win.JS or this case SDKSample.Scenario which the  developer created in sample-utils.js (which will be discussed in another blog post).

<div data-win-control="SdkSample.ScenarioInput">  

SdkSample.ScenarioInput  control inserts the appropriate markup to get controls place into the input  section of the page. 

and

<div data-win-control="SdkSample.ScenarioOutput">  

SdkSample.ScenarioOutput control inserts the appropriate markup to et controls place into the output section of the page.

Scenario1.JS

Create variable

var page = WinJS.UI.Pages.define("/html/scenario1.html", {          ready: function (element, options) {….

 

WinJS.UI.Pages.define :   each page loaded is defined using this method.  It allows  the app to define what should happen when you navigate to a page.

 

Create File Function ( createFile() )

In this function I want to you to pay attention to the  snippet below
       Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("sample.dat", Windows.Storage.CreationCollisionOption.replaceExisting).done(…

Earlier in this post I mentioned that we would be using  a couple of Windows.Storage.Api s.  Windows.storage.namespace is made of the following types: members, classes, delegates, enumerations, and interfaces.

Let’s take the snippet above find out what each section does 

  • KnowFolders :   is a class  in Windows.storage that provides access to common locations that contains content.
  • documentsLibrary:   where we want the file to be created.
  • CreateFileAsync is a storagefolder methods can be creates new file in a folder or group.  Before we continue there are two ways this method can be used:
    • CreateFileAsync(String) – which is used to create a new file in folder or file group.
    • CreateFileAsync(String,CreationCollisionOption.replaceExisting.done)- as seen in the snippet above creates a new file in a current folder and specifies what do if a file with the same name already exist.

Run code

Results

image

 

Lesson 2 – will follow up with scenarios 2 -4