Isolated Storage Vista and .NET 2.0

Many of you maybe have playing around with Vista Development when the sdk become RTM. As we know Vista becoming more and more aware of security, doesn't thinking Vista will give you administrator right for every action that you take? Vista thinks sandbox is better for security, and .NET is even better. When we build the windows form application, the application store user setting in local user storage and store application setting in machine storage (app.config where the application installation directory located). It would be nice to have a place to store information that was safe to use without having to test whether the application has enough rights to save data to the hard drive. That solution is what isolated storage is designed to provide.

Before Vista released with so many stuff in security, Visual Studio 2005 have provided us a good deployment solution called COD (Click Once Deployment) (some people said it's Smart Client Application...but for sure Smart Client Application is NOT JUST Click Once Deployment). If you want playing around with application configuration use ConfigurationManager class (just like before and it's seemly work in Vista). But if you want to store another things outside the configuration (although in many cases Configuration object is enough) you can store in isolated storage rather than save it in hardwire manner (i.e. C:\\UserFile.txt).

Isolated storage is handled by IsolatedStorageFile family (there are some of them you can refer into MSDN). However to create Isolated storage is not more than fun, below is snippet code that show 101 operation about Isolated storage

// it's better approach to save the file in user store location some location in C:\\User\NameUSer if in Vista

// another option is in GetMachineStoreForAssembly(); it will save into machine level

IsolatedStorageFile userStore =

IsolatedStorageFile.GetUserStoreForAssembly();

// name file, mode creation, and reference object

IsolatedStorageFileStream userStream =

new IsolatedStorageFileStream("isolated.rf",

FileMode.Create,

userStore);

// C# let's write something, use another File IO class for alternative writing mechanishm

StreamWriter userWriter = new StreamWriter(userStream);

userWriter.WriteLine("Hello from Vista");

userWriter.Close();

// let's read it

string[] files = userStore.GetFileNames("isolated.rf");
if (files.Length == 0)
{
    Console.WriteLine("ora ono datane dab piye ki");
}
else
{
    // ...
}

You can also create directory by using CreateDirectory() or GetDirectoryNames(), furthermore you can set quota policy and access permisiion in this isolated storage by using atribute in the method body.

// need security action in code, with 2048 K data
[IsolatedStorageFilePermission(SecurityAction.Demand,
UserQuota=2048,
UsageAllowed=IsolatedStorageContainment.AssemblyIsolationByUser)]
class HelloVista
{
    // ...
}

But be careful this atribute need some of code acess security to perform it, so be sure you have configure it in caspol using costum action in MSI installer or publishing option in Clik Once

Happy Coding with Vista J

Best Regards,

Ridi Ferdiana

Share this post: | | | |
Filed under:

Comments

# stupid said:

seriously, I prefer the point rather than  <a href=www.bawwgt.com/game story power leveling</a>

Monday, September 22, 2008 5:17 PM