Browse DevX
Sign up for e-mail newsletters from DevX


Use Isolated Storage for Stateful Network-deployable .NET Components : Page 4

Isolated storage is a .NET security feature that assigns each application a unique storage area that is fully isolated from other applications and essentially private. Isolated storage provides true isolation in the sense that the identity of an application or a component uniquely determines the root of a virtual, sandboxed file system. This form of data storage is well suited to partially trusted applications in general and ASP.NET applications in particular.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using Isolated Streams
The IsolatedStorageFile object exposes a few methods to manipulate files and directories. In particular, the CreateDirectory method lets you create a new directory in the store.


You can indicate multiple levels of directories and CreateDirectory will ensure that the specified path exists when it returns. You read, write, and create files using a particular flavor of stream—the IsolatedStorageFileStream class. The file name must be relative and cannot contain any drive information. Look at this example:

Dim stm As IsolatedStorageFileStream stm = New IsolatedStorageFileStream(file, _ FileMode.Create, iso) Dim s As String = "Hello, world!" stm.Write(Encoding.ASCII.GetBytes(s), _ 0, s.Length) stm.Close()

You use the file stream API to create a file. The file stream API is based on the programming interface of the FileStream class from which the IsolatedStorageFileStream class is derived. Once you hold an IsolatedStorageFileStream object you can use it to work with a StreamReader or a StreamWriter object, both of which provide an easier and more programmer-friendly set of methods. Using StreamReader and StreamWriter you can read and write to a file in a store as you would to any other file.

Dim reader As StreamReader reader = New StreamReader(stm) Console.WriteLine(reader.ReadToEnd()) reader.Close()

Although the isolated storage is a miniaturized version of the operating system's file system, it does not provide a rich programming interface. Isolated storage provides for the key tasks but not always through powerful and easy-to-use tools. If you want to test to make sure a given file exists before you overwrite it you must run a For-Each statement on an array of strings. Here is how:

Dim filenames As String() filenames = iso.GetFileNames("test.txt") Dim file As String For Each file In filenames If file = "test.txt" Then Console.WriteLine("Already exists!") Exit Sub End If Next

GetFileNames on the IsolatedStorageFile class returns an array of strings filled with all the file names found in the specified pattern. GetFileNames takes an expression that may contain wildcard characters such as ? or *. Wildcards are permitted only in the file name portion of the expression. You cannot use wildcards to indicate a directory path. The returned array contains only the name of items found—that's why you can't perform your search on multiple paths. Similarly, GetDirectoryNames selects all directories in the isolated storage that match the pattern. Both methods require an instance of the Store object (they are not static methods) and don't work recursively on the subfolders of the specified folder.

DeleteDirectory and DeleteFile are instance methods of the IsolatedStorageFile class that allow you to remove undesired storage directories and files. These Delete methods do not accept wildcards. To remove the whole store you use the Remove method. It is important that you remove unneeded files and directories because the disk reserved for isolated storage is not unlimited. Administrators must tune the quotas of disk storage allocated to each single store. Two read-only long properties—MaximumSize and CurrentSize—let developers foresee in advance whether a given call may fail due to space constraints. Notice that quotas are assigned on a per-evidence basis, which means that the same assembly can receive different quota if run from an intranet rather than from the Internet.

Isolated storage is a built-in mechanism of .NET security that let potentially unsecure applications safely access the disk and the local file system to perform simple writing operations. The idea behind isolated storage is the sandbox—the same that secures Java applications within the boundaries of the Java virtual machine. Isolated storage provides full disk access to applications but below isolated file system subtrees. Partially trusted applications can use isolated storage to persist data without the risk of performing malicious operations against sensitive parts of the operating system. Writing partially trusted applications is a delicate art that goes far beyond the functionality of isolated storage. But that is another story and probably another article!

Dino Esposito is Wintellect's ADO.NET and XML expert and a trainer and consultant based in Rome, Italy. Dino is the author of "Building Web Solutions with ASP.NET" and "ADO.NET and Applied XML Programming for Microsoft .NET," both from Microsoft Press. .
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date