Jypeli  5
The simple game programming library
IsolatedStorageManager.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO.IsolatedStorage;
6 using System.IO;
7 
8 namespace Jypeli
9 {
11  {
13  {
14  _currentDir = ".";
15  }
16 
22  public override bool Exists( string fileName )
23  {
24  var userStore = FMAssert( IsolatedStorageFile.GetUserStoreForApplication, false, null );
25  if ( userStore == null ) return false;
26 
27  MakeAbsolute( ref fileName );
28  return FMAssert( userStore.FileExists, false, false, fileName );
29  }
30 
36  public override bool ChDir( string path )
37  {
38  var userStore = FMAssert( IsolatedStorageFile.GetUserStoreForApplication, false, null );
39  if ( userStore == null ) return false;
40 
41  if ( !FMAssert( userStore.DirectoryExists, false, false, path ) )
42  return false;
43 
44  _currentDir = path;
45  return true;
46  }
47 
52  public override void MkDir( string path )
53  {
54  var userStore = FMAssert( IsolatedStorageFile.GetUserStoreForApplication, false, null );
55  if ( userStore == null ) return;
56 
57  MakeAbsolute( ref path );
58  FMAssert( userStore.CreateDirectory, true, path );
59  }
60 
65  public override void RmDir( string path )
66  {
67  var userStore = FMAssert( IsolatedStorageFile.GetUserStoreForApplication, false, null );
68  if ( userStore == null ) return;
69 
70  MakeAbsolute( ref path );
71  FMAssert( userStore.DeleteDirectory, true, path );
72  }
73 
74  public override IList<string> GetFileList()
75  {
76  var userStore = FMAssert( IsolatedStorageFile.GetUserStoreForApplication, false, null );
77  if ( userStore == null ) return new List<string>();
78 
79  string[] fileList = FMAssert( userStore.GetFileNames, false, new string[] { }, _currentDir + "\\*" );
80  return fileList.ToList<string>().AsReadOnly();
81  }
82 
89  public override StorageFile Open( string fileName, bool write )
90  {
91  MakeAbsolute( ref fileName );
92  var stream = FMAssert( openFileStream, write, null, fileName, write );
93  return new StorageFile( fileName, stream );
94  }
95 
96  private Stream openFileStream( string fileName, bool write )
97  {
98  var userStore = IsolatedStorageFile.GetUserStoreForApplication();
99  FileMode mode = write ? FileMode.Create : FileMode.Open;
100  FileAccess access = write ? FileAccess.ReadWrite : FileAccess.Read;
101  return userStore.OpenFile( fileName, mode, access );
102  }
103 
108  public override void Delete( string fileName )
109  {
110  var userStore = FMAssert( IsolatedStorageFile.GetUserStoreForApplication, false, null );
111  if ( userStore == null ) return;
112 
113  MakeAbsolute( ref fileName );
114  FMAssert( userStore.DeleteFile, true, fileName );
115  }
116  }
117 }
void FMAssert(Action func, bool write)
Definition: Assert.cs:26
override void Delete(string fileName)
Poistaa tiedoston.
override bool Exists(string fileName)
Kertoo onko tiedosto tai hakemisto olemassa.
override bool ChDir(string path)
Vaihtaa työhakemistoa.
override void MkDir(string path)
Luo uuden hakemiston.
override void RmDir(string path)
Tuhoaa hakemiston. Heittää poikkeuksen jos hakemisto ei ole tyhjä.
override StorageFile Open(string fileName, bool write)
Avaa tiedoston.
override IList< string > GetFileList()