DESKTOP

Store Data when Your App Has Restricted Permissions

9/25/2010 3:24:03 PM
Problem : You need to save application and user data even when the app has no permission to access the local computer, such as when it’s running from the Internet security zone.

Solution:
Use Isolated Storage. This is kind of like a virtual file system that .NET associates with an assembly, user, application domain, application (when using ClickOnce only), or a combination of these items. By using Isolated Storage, you can give programs the ability to store information without giving them access to the real file system.Here’s a simple example that creates a subdirectory and a text file:
using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace IsolatedStorageDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Run with command line arg -r to remove isolated storage for this app/user");

//get the isolated storage for this appdomain + user
using (IsolatedStorageFile file =
IsolatedStorageFile.GetUserStoreForDomain())
{
//setup directory
if (!file.DirectoryExists("Dummy"))
{
file.CreateDirectory("Dummy");
}
Console.WriteLine("Accesses:");
//read and write to a file in the directory
using (IsolatedStorageFileStream stream =
file.OpenFile(@"Dummy\accesses.txt",
System.IO.FileMode.OpenOrCreate))
using (TextReader reader = new StreamReader(stream))
using (TextWriter writer = new StreamWriter(stream))
{
string line = null;
do
{
line = reader.ReadLine();
if (line != null)
{
Console.WriteLine(line);
}
} while (line != null);

writer.WriteLine(DateTime.Now.ToString());
}
if (args.Length > 0 && args[0] == "-r")
{
Console.WriteLine( "Removing isolated storage for this user/app-domain");
file.Remove();
}
}

Console.ReadKey();
}
}
}


After a few runs, the output looks like this:

Run with command line arg -r to remove isolated storage
for this app/user
Accesses:
7/4/2009 4:02:08 PM
7/4/2009 4:02:16 PM
7/4/2009 4:02:22 PM
7/4/2009 4:02:28 PM

Other  
 
Most View
Samsung Home Theatre Series With Wireless Connection
MSI GT70 Notebook - Power Play
Compact Cameras For The Dynamic
Review: lcy Box Stand For iPad
Better Than Expected Showing (Part 2)
G Data Total Protection 2013 - Innovative Fingerprinting Technique
ECS Z77H2-A2X v1.0 - Golden LGA 1155 Mainboard From The Black Series (Part 3)
Free VirtualBox Images (Part 2) - Create your own VirtualBox image
Improve Your Mac (Part 4) - Tips for saving bandwidth
The Modern Office (Part 2)
Top 10
Kingston Wi - Drive 128GB: Simple To Get Started
Seagate Wireless Plus 1 TB - Streaming Videos To Various Devices
Seagate Wireless Plus 1TB - Seagate's Second Wireless External Hard Drive
Western Digital My Passport 2TB - The Ideal Companion For Anyone
Lenovo IdeaTab A2109 - A Typical Midrange Android Tablet
Secret Tips For Your Kindle Fire
The Best Experience With Windows 8 Tablets And Hybrids (Part 2)
The Best Experience With Windows 8 Tablets And Hybrids (Part 1)
Give Your Browser A Health Check
New Ways To Block Irritating Ads…