[C#]
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;
public class FindingExistingFilesAndDirectories{
// Retrieves an array of all directories in the store, and
// displays the results.
public static void Main(){
// This part of the code sets up a few directories and files in the
// store.
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
isoStore.CreateDirectory("TopLevelDirectory");
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
// End of setup.
Console.WriteLine('\r');
Console.WriteLine("Here is a list of all directories in this isolated store:");
foreach(string directory in GetAllDirectories("*", isoStore)){
Console.WriteLine(directory);
}
Console.WriteLine('\r');
// Retrieve all the files in the directory by calling the GetFiles
// method.
Console.WriteLine("Here is a list of all the files in this isolated store:");
foreach(string file in GetAllFiles("*", isoStore)){
Console.WriteLine(file);
}
}// End of Main.
// Method to retrieve all directories, recursively, within a store.
public static string[] GetAllDirectories(string pattern, IsolatedStorageFile storeFile){
// Get the root of the search string.
string root = Path.GetDirectoryName(pattern);
if (root != "") root += "/";
// Retrieve directories.
string[] directories;
directories = storeFile.GetDirectoryNames(pattern);
ArrayList directoryList = new ArrayList(directories);
// Retrieve subdirectories of matches.
for (int i = 0, max = directories.Length; i < max; i++){
string directory = directoryList[i] + "/";
string[] more = GetAllDirectories (root + directory + "*", storeFile);
// For each subdirectory found, add in the base path.
for (int j = 0; j < more.Length; j++)
more[j] = directory + more[j];
// Insert the subdirectories into the list and
// update the counter and upper bound.
directoryList.InsertRange(i+1, more);
i += more.Length;
max += more.Length;
}
return (string[])directoryList.ToArray(Type.GetType("System.String"));
}
public static string[] GetAllFiles(string pattern, IsolatedStorageFile storeFile){
// Get the root and file portions of the search string.
string fileString = Path.GetFileName(pattern);
string[] files;
files = storeFile.GetFileNames(pattern);
ArrayList fileList = new ArrayList(files);
// Loop through the subdirectories, collect matches,
// and make separators consistent.
foreach(string directory in GetAllDirectories( "*", storeFile))
foreach(string file in storeFile.GetFileNames(directory + "/" + fileString))
fileList.Add((directory + "/" + file));
return (string[])fileList.ToArray(Type.GetType("System.String"));
}// End of GetFiles.
}
[C#]
using System;
using System.IO;
using System.IO.IsolatedStorage;
public class ReadingAndWritingToFiles{
public static int Main(){
// Get an isolated store for this assembly and put it into an
// IsolatedStoreFile object.
IsolatedStorageFile isoStore =IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
// This code checks to see if the file already exists.
string[] fileNames = isoStore.GetFileNames("TestStore.txt");
foreach (string file in fileNames){
if(file == "TestStore.txt"){
Console.WriteLine("The file already exists!");
Console.WriteLine("Type \"StoreAdm /REMOVE\" at the command line to delete all Isolated Storage for this user.");
// Exit the program.
return 0;
}
}
writeToFile(isoStore);
Console.WriteLine("The file \"TestStore.txt\" contains:");
// Call the readFromFile and write the returned string to the
//console.
Console.WriteLine(readFromFile(isoStore));
// Exit the program.
return 0;
}// End of main.
// This method writes "Hello Isolated Storage" to the file.
private static void writeToFile(IsolatedStorageFile isoStore){
// Declare a new StreamWriter.
StreamWriter writer = null;
// Assign the writer to the store and the file TestStore.
writer = new StreamWriter(new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew,isoStore));
// Have the writer write "Hello Isolated Storage" to the store.
writer.WriteLine("Hello Isolated Storage");
writer.Close();
Console.WriteLine("You have written to the file.");
}// End of writeToFile.
// This method reads the first line in the "TestStore.txt" file.
public static String readFromFile(IsolatedStorageFile isoStore){
// This code opens the TestStore.txt file and reads the string.
StreamReader reader = new StreamReader(new IsolatedStorageFileStream("TestStore.txt", FileMode.Open,isoStore));
// Read a line from the file and add it to sb.
String sb = reader.ReadLine();
// Close the reader.
reader.Close();
// Return the string.
return sb.ToString();
}// End of readFromFile.
}
[C#]
using System;
using System.IO.IsolatedStorage;
using System.IO;
public class DeletingFilesDirectories{
public static void Main(){
// Get a new isolated store for this user domain and assembly.
// Put the store into an isolatedStorageFile object.
IsolatedStorageFile isoStore =IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
Console.WriteLine("Creating Directories:");
// This code creates several different directories.
isoStore.CreateDirectory("TopLevelDirectory");
Console.WriteLine("TopLevelDirectory");
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
Console.WriteLine("TopLevelDirectory/SecondLevel");
// This code creates two new directories, one inside the other.
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory");
Console.WriteLine();
// This code creates a few files and places them in the directories.
Console.WriteLine("Creating Files:");
// This file is placed in the root.
IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
Console.WriteLine("InTheRoot.txt");
isoStream1.Close();
// This file is placed in the InsideDirectory.
IsolatedStorageFileStream isoStream2 = new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
Console.WriteLine();
isoStream2.Close();
Console.WriteLine("Deleting File:");
// This code deletes the HereIAm.txt file.
isoStore.DeleteFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
Console.WriteLine();
Console.WriteLine("Deleting Directory:");
// This code deletes the InsideDirectory.
isoStore.DeleteDirectory("AnotherTopLevelDirectory/InsideDirectory/");
Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/");
Console.WriteLine();
}// End of main.
}
……