As we know, WinJS applications are built using HTML/JS/CSS. And for a complete application, we will need async operations like SQLite database operations, web-service calls, and other native XML read-write, and file read-write which cannot be done directly in WinJS applications.
So we have to use Windows Runtime Component to access Native C# methods as discussed in my previous article,
WinJS application with Windows Runtime Component to access Native C# Code
I will show you how to create a text file and write some content in it. After that I will show you how to read that file and display the file content in our HTML page.
Step 1:
Firstly, create a WinJS project as discussed in in my following article. For that we will start with creating a blank Universal Windows App: Please have a look in my previous article: Create Your First WinJS Application
Step 2:
We need to do following things as discussed in my previous article,
- Add a Windows Runtime Component project and name it ‘WinRuntimes’
- Delete default class ‘Class1.cs’
- Add a new class and name it ‘Service’
- Add WinRuntimes reference in WinJS project
- Build the project
Step 3:
In Service class, we have to create IAsyncOperation for CreateFile() and ReadFile(). IAsyncOperation represents an asynchronous operation, which returns a result upon completion. This is the return type for many Windows Runtime asynchronous methods that have results but don't report progress. Find more in MSDN.
In CreateFile function, we will pass fileName and fileContent as parameters whereas in ReadFile, we pass filename with the return type string to return the filecontent.
Complete code snippet for Service class:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Windows.Foundation;
- using Windows.Storage;
- namespace WinRuntimes
- {
- public sealed class Service
- {
- public IAsyncOperation<string> CreateFile(string fileName, string fileContent)
- {
- return CreateFileHelper(fileName, fileContent).AsAsyncOperation();
- }
- public IAsyncOperation<string> ReadFile(string fileName)
- {
- return ReadFileHelper(fileName).AsAsyncOperation();
- }
- private async Task<string> CreateFileHelper(string fileName, string fileContent)
- {
- try
- {
- fileName = fileName + ".txt";
- StorageFolder folder = ApplicationData.Current.LocalFolder;
- StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
- await FileIO.WriteTextAsync(file, fileContent);
- return "success";
- }
- catch (Exception ex)
- {
- return "fail";
- }
- }
- private async Task<string> ReadFileHelper(string fileName)
- {
- try
- {
- fileName = fileName + ".txt";
- StorageFolder folder = ApplicationData.Current.LocalFolder;
- StorageFile file = await folder.GetFileAsync(fileName);
- string fileContent = await FileIO.ReadTextAsync(file);
- return fileContent;
- }
- catch (Exception ex)
- {
- return "fail";
- }
- }
- }
- }



NitinPosted Apr 22, 2016, 11:49 PM
Good one
Kuppurasu NagarajPosted Apr 22, 2016, 12:05 PM
Nice Sharing..
Bhuvanesh MohankumarPosted Apr 21, 2016, 2:26 PM
Nice
Debasis SahaPosted Apr 21, 2016, 1:28 PM
Nice One..
Rahul Kumar SaxenaPosted Apr 21, 2016, 9:07 AM
Good one
Vignesh ManiPosted Apr 21, 2016, 8:21 AM
Nice