public static Document CreateFileFromString(string path) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { string[] pathArray = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries); string currPath = string.Empty; // Skip the last because it's the filename for (int i = 0; i < pathArray.Length - 1; i++) { currPath = System.IO.Path.Combine(currPath, pathArray[i]); if (!isf.DirectoryExists(currPath)) isf.CreateDirectory(currPath); } string fileName = pathArray[pathArray.Length - 1]; IsolatedStorageFileStream f = isf.CreateFile(System.IO.Path.Combine(currPath, fileName)); f.Close(); Path p = new Path(PathBase.Root); // Skip the first because "root" is already in the path. for (int i = 1; i < pathArray.Length; i++) p = p.NavigateIn(pathArray[i]); return new Document(p); } }
private static void DeleteRecursive(Path dir, IsolatedStorageFile isf) { // Delete every subdirectory's contents recursively foreach (string subDir in isf.GetDirectoryNames(dir.PathString + "/*")) DeleteRecursive(dir.NavigateIn(subDir), isf); // Delete every file inside foreach (string file in isf.GetFileNames(dir.PathString + "/*")) isf.DeleteFile(System.IO.Path.Combine(dir.PathString, file)); isf.DeleteDirectory(dir.PathString); }
// adds " ($i)" to the end of the given name to make it a unique name public static string GetUniqueDirectoryName(Path parent, string originalName) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { int i = 1; string newName = originalName; if (isf.DirectoryExists(parent.NavigateIn(newName).PathString)) { do { newName = originalName + " (" + i + ")"; } while (isf.DirectoryExists(parent.NavigateIn(newName).PathString)); } return newName; } }