public async Task OpenAppFile(string fileName) { var removableDevices = KnownFolders.RemovableDevices; var externalDrives = await removableDevices.GetFoldersAsync(); var usbDrive = externalDrives.Single(e => e.DisplayName.Contains("USB DISK")); StorageFile appconfig = await usbDrive.CreateFileAsync( string.Format("{0}.jpg", fileName), CreationCollisionOption.OpenIfExists); using (StreamReader reader = new StreamReader(await appconfig.OpenStreamForReadAsync())) { var data = await reader.ReadToEndAsync(); AppData = Newtonsoft.Json.JsonConvert.DeserializeObject <MyAppData>(data); } }
public async Task OpenAppFile(string fileName) { try // Create a local files (open and add text if exist) for writing a short text { // Create sample file, open if exists string fn = fileName + "-Sequensial.txt"; Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync(fn, Windows.Storage.CreationCollisionOption.OpenIfExists); // Writing to the file await Windows.Storage.FileIO.AppendTextAsync(sampleFile, "Swift as a shadow" + Environment.NewLine); } catch (Exception ex) { DebugTextBox.Text += ex.Message; } try // Create a local files (open and add text if exist) for writing a short text { // Create sample stream file, open if exists string fn = fileName + "-Stream.txt"; Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync(fn, Windows.Storage.CreationCollisionOption.OpenIfExists); var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); using (var outputStream = stream.GetOutputStreamAt(stream.Size)) { // We'll add more code here in the next step. using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream)) { dataWriter.WriteString("DataWriter has methods to write to various types, such as DataTimeOffset." + Environment.NewLine); await dataWriter.StoreAsync(); await outputStream.FlushAsync(); } } stream.Dispose(); // Or use the stream variable (see previous code snippet) with a using statement as well. } catch (Exception ex) { DebugTextBox.Text += ex.Message; } try // Initial/create OneDrive stream for write/append text { // Create file Stream towards OneDrive datastore } catch (Exception ex) { DebugTextBox.Text += ex.Message; } //OK, then we are ready to read/write file /* try // Try to open file at USB drive * { * var removableDevices = KnownFolders.RemovableDevices; * var externalDrives = await removableDevices.GetFoldersAsync(); * var usbDrive = externalDrives.Single(e => e.DisplayName.Contains("USB DISK")); * string fn = usbDrive.Name + string.Format("{0}.jpg", fileName); * * DebugTextBox.Text += "Will try to open:" + fn + " on USB Drive:" + usbDrive.Name; * * StorageFile appconfig = await usbDrive.CreateFileAsync( * fn, * CreationCollisionOption.OpenIfExists); * * using (StreamReader reader = new StreamReader(await appconfig.OpenStreamForReadAsync())) * { * var data = await reader.ReadToEndAsync(); * AppData = Newtonsoft.Json.JsonConvert.DeserializeObject<MyAppData>(data); * } * } * catch (Exception ex) * { * // error * DebugTextBox.Text += ex.Message; * } */ try { // Open and read the file // Create/open sample file; replace if exists. string fn = fileName + ".jpg"; Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; DebugTextBox.Text += "Will try to open: " + fn + " in " + storageFolder.Path; StorageFile appconfig = await storageFolder.CreateFileAsync(fn, Windows.Storage.CreationCollisionOption.OpenIfExists); using (StreamReader reader = new StreamReader(await appconfig.OpenStreamForReadAsync())) { var data = await reader.ReadToEndAsync(); AppData = Newtonsoft.Json.JsonConvert.DeserializeObject <MyAppData>(data); } } catch (Exception ex) { // Exceptopn DebugTextBox.Text += ex.Message; } }