[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.
}
新聞熱點
疑難解答
圖片精選