public static async Task <string> ReadFile(string filename, string folderPath = "DataCache", bool isInstallationFolder = false) { StorageFolder localFolder; if (isInstallationFolder) { localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; } else { localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; } var folder = await localFolder.GetFolderAsync(folderPath); //var folder = await installFolder.GetFolderAsync("Assets/Constellation"); var file = await folder.GetFileAsync(filename); var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var inStream = fs.GetInputStreamAt(0); Windows.Storage.Streams.DataReader reader = new Windows.Storage.Streams.DataReader(inStream); await reader.LoadAsync((uint)fs.Size); string data = reader.ReadString((uint)fs.Size); reader.DetachStream(); return(data); }
public static async Task <string> ReadFile(string filename) { var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; var folder = localFolder; var file = await folder.GetFileAsync(filename); var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var inStream = fs.GetInputStreamAt(0); Windows.Storage.Streams.DataReader reader = new Windows.Storage.Streams.DataReader(inStream); await reader.LoadAsync((uint)fs.Size); string data = reader.ReadString((uint)fs.Size); reader.DetachStream(); return(data); }
private async Task <Stream> ReadFile(string filename) { var file = await Folder.GetFileAsync(filename); if (!file.IsAvailable) { this.UpdateCache().ConfigureAwait(false); return(null); } var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var inStream = fs.GetInputStreamAt(0); var reader = new Windows.Storage.Streams.DataReader(inStream); await reader.LoadAsync((uint)fs.Size); var data = reader.ReadBuffer((uint)fs.Size); reader.DetachStream(); return(data.AsStream()); }
/// <summary> /// Creates and adds a time track objects into the Items collection. /// </summary> public async void LoadData() { var fileList = await ApplicationData.Current.LocalFolder.GetFilesAsync(); var file = fileList.FirstOrDefault(f => f.Name == localFileName); if (file != null) { using (var stream = await file.OpenReadAsync()) { using (var dr = new Windows.Storage.Streams.DataReader(stream)) { try { await dr.LoadAsync(unchecked((uint)stream.Size)); var numberOfItems = dr.ReadInt32(); ObservableCollection<TrackItem> loadingItems = new ObservableCollection<TrackItem>(); for (int x = 0; x < numberOfItems; x++) { TrackItem item = new TrackItem(); item.Start = dr.ReadDateTime(); item.End = dr.ReadDateTime(); int topicLength = dr.ReadInt32(); item.Topic = dr.ReadString((uint)topicLength); loadingItems.Add(item); } bool currentItemExists = dr.ReadBoolean(); TrackItem loadingCurrentItem = null; if (currentItemExists) { loadingCurrentItem = new TrackItem(); loadingCurrentItem.Start = dr.ReadDateTime(); int topicLength = dr.ReadInt32(); loadingCurrentItem.Topic = dr.ReadString((uint)topicLength); } dr.DetachStream(); Items = loadingItems; OnPropertyChanged(nameof(Items)); CurrentItem = loadingCurrentItem; IsDataLoaded = true; } catch { try { await file.DeleteAsync(); } catch { } CurrentItem = null; IsDataLoaded = true; } } } } else { // Sample data; replace with real data Items.Add(new TrackItem() { Topic = "First Tracked Item", Start = new DateTimeOffset(new DateTime(2015, 11, 5, 9, 0, 0), new TimeSpan(-6, 0, 0)), End = new DateTimeOffset(new DateTime(2015, 11, 5, 11, 30, 0), new TimeSpan(-6, 0, 0)), }); Items.Add(new TrackItem() { Topic = "Second Tracked Item", Start = new DateTimeOffset(new DateTime(2015, 11, 5, 12, 30, 0), new TimeSpan(-6, 0, 0)), End = new DateTimeOffset(new DateTime(2015, 11, 5, 17, 0, 0), new TimeSpan(-6, 0, 0)), }); CurrentItem = null; this.IsDataLoaded = true; } }