Sunday, August 16, 2009

Creating Isolated Storage Files

Introduction

Many programmers use the config file to keep the application configuration data. But one disadvantage with this is, it is a read only mechanism. You cannot update data in application config files, using the ConfigurationSettings classes in .NET. In earlier days, .ini files or registry was used to save application specific data.

Of course, we can use regular files to save application data. But now the question is how to protect the data, where to save it and all other mess!

Isolated Storage

.NET introduces a concept called Isolated Storage. Isolated Storage is a kind of virtual folder. Users never need to know where exactly the file is stored. All you do is, tell the .NET framework to store your file in Isolated Storage. The physical location of Isolated Storage varies for each Operating System. But your application simply uses the .NET classes to create and access files, without bothering where it is physically located. And you can have Isolated Storage specific to each Assembly or each Windows user.

The following code sample demonstrates the basic create, write and read operations with Isolated Storage.

Make sure you include the following directives on top of your file:

using System.IO;
using System.IO.IsolatedStorage;
using System.Diagnostics;
public static void Main(string[] args)
{
const string ISOLATED_FILE_NAME = "MyIsolatedFile.txt";

//-------------------------------------------------------


// Check if the file already exists in isolated storage.


//-------------------------------------------------------


IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore( IsolatedStorageScope.User
| IsolatedStorageScope.Assembly, null, null );

string[] fileNames = isoStore.GetFileNames( ISOLATED_FILE_NAME );

foreach ( string file in fileNames )
{
if ( file == ISOLATED_FILE_NAME )
{
Debug.WriteLine("The file already exists!");
}
}

//-------------------------------------------------------


// Write some text into the file in isolated storage.


//-------------------------------------------------------


IsolatedStorageFileStream oStream =
new IsolatedStorageFileStream( ISOLATED_FILE_NAME,
FileMode.Create, isoStore );

StreamWriter writer = new StreamWriter( oStream );
writer.WriteLine( "This is my first line in the isolated storage file." );
writer.WriteLine( "This is second line." );
writer.Close();

//-------------------------------------------------------


// Read all lines from the file in isolated storage.


//-------------------------------------------------------


IsolatedStorageFileStream iStream =
new IsolatedStorageFileStream( ISOLATED_FILE_NAME,
FileMode.Open, isoStore );

StreamReader reader = new StreamReader( iStream );
String line;

while ( (line = reader.ReadLine()) != null )
{
Debug.WriteLine( line );
}

reader.Close();
}

What is Isolate Storage

We all know that all applications need some storage space to archive certain application information like users preferences, appliction info, or certain data which can be cached on the user's local machine etc. Initially all this information was stored in .ini files which were simple to use and manage. Being a flat file their access was easy and Microsoft also provided us with
API's to access these files. But the only problem with .ini files was where do u put these ini files and if the files were lost it would result in an application nightmare.

To solve this problem microsoft came up with an another idea for storing application related information i.e. The Windows Registry. Although registry was good for storing application related information it was very slow coz of its hierarcial nature. Loading information of all the applications in the machine's registry made the search slow. Secondly application downloaded
from the web can not be granted access to the local machines registry. Hence it could not be used for web apps.

To solve all the above problems Micorsoft with .NET has now come up with the idea of Isolated Storage.

Isolated storage enables paritally trusted applications to store data as per computer's security policy esp. the web app's and other downloaded components. Code running on the local computer , local network or the Internet is granted the right to use this isolated storage.


Isolated storage isolates data by user or by assembly. Credentials such as the origin, strong name of the assembly or application domain can be used to isolate data.

But what is isolated storage ?Well Isolated storage is a special folder on your harddisk which allows your application to store its application specific data. Isolated stores are put up at Microsoft\IsolatedStorage (Win 98) directory.Change folder settings to show hidden files and folders in order to see isolated storage.

On Windows 2000 you will be able to view "IsolatedStorage" folder at :-

Roaming-enabled stores = \Documents and Settings\\Application Data

Non-roaming stores = \Documents and Settings\\Local Settings\Application Data

All applications have a special allocated place to store their data which is a data folder containing one or more isolated storage files, called stores in this folder. These stores contain the actual directory locations where data is stored. The data saved in the store can be any kind of data from user's preference information to application state.

To access isolated storage the code must be have appropriate IsolatedStorageFilePermissions and all necessary native platform operating system rights.

e.g. On an OS like Win 2000 the access control lists (ACLs) controls which users have the rights to use the file system. Hence web applications don't have access to the local hard disk. But these applications have access to the Isolated storage folder on the client machine to store any application data. Thus inspite of not having access to the local harddisk web app can
store data related to their application in the IsolatedStorage folder.

Microsoft .NET Framework applications already have operating system rights to access isolated storage unless they perform impersonation.


Let us use the storeadm.exe tool to list ,add and delete isolated stores.

1) You can go to the VS.NET command prompt and type storeadm.exe /? to view
all the options.

Usage : StoreAdm [options]

options :[/list][/REMOVE] [/ROAMING] [/QUIET]

/LIST : Displays the existing isolated storage for the current user.

/REMOVE : Removes all existing isolated storage for the current user.

/ROAMING : Select the roaming store.

/QUIET : Only error messages will be output.

2) Use the storeadm /LIST option to list all the stores on your machine. This will list all the stores in your local machine.

You can use isolated storage just like any other file system folder. There are special IsolatedStorageFileStreams to create files and directories. Later we will see how we can create these isolated stores.

Sunday, January 25, 2009

How to Add Confirmation Dialog on the Delete Button in a GridView

I have searched and visited a lot of code on adding confirmation dialog on the Delete control in a GridView in ASP.NET. Among those I found the code snippet below very easy and small.

Write the code below on the RowDataBound event of the GridView.

Assuming that the DeleteButton's type is LinkButton and the it is in the first column( the index of column is 0).
--------------------Code Starts-------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes["onClick"] = "if(!confirm('Are you sure to delete this row?'))return false;";

// If DeleteButton's type is Button, you can use (Button) in stead of (LinkButton).
}
}
}

-------------------Code Ends-------------------------------