//Windows.Storage.StorageFolder RecvFolder = null; //private async Task SaveAFile3(FileDetail rcvFile) //{ // if (RecvFolder==null) // { // PostMessage("SaveAFile3", "Operation cancelled as no Recv folder."); // return; // } // Windows.Storage.StorageFile file = await RecvFolder.CreateFileAsync(rcvFile.filename,Windows.Storage.CreationCollisionOption.GenerateUniqueName); // if (file == null) // { // PostMessage("SaveAFile3", "Operation cancelled as failed to create file."); // return; // } // // Prevent updates to the remote version of the file until // // we finish making changes and call CompleteUpdatesAsync. // Windows.Storage.CachedFileManager.DeferUpdates(file); // // write to file // await Windows.Storage.FileIO.WriteTextAsync(file, rcvFile.txt); // // Let Windows know that we're finished changing the file so // // the other app can update the remote version of the file. // // Completing updates may require Windows to ask for user input. // Windows.Storage.Provider.FileUpdateStatus status = // await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); // if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) // { // PostMessage("File ", file.Name + " was saved in \r\n" + file.Path); // } // else // { // PostMessage("File ", file.Name + " couldn't be saved."); // } //} internal async void SaveFile(FileDetail rcvFile) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { await SaveAFile2(rcvFile); }); }
// private async Task SaveAFile(string txt) // { // bool usedPicker = true; // FileSavePicker savePicker = null; // Windows.Storage.StorageFile file = null; //#if IoTCore //#else // savePicker = new Windows.Storage.Pickers.FileSavePicker(); //#endif // if (savePicker != null) // { // savePicker.SuggestedStartLocation = // Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; // // Dropdown of file types the user can save the file as // savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" }); // // Default file name if the user does not type one in or select a file to replace // savePicker.SuggestedFileName = "New Document"; // file = await savePicker.PickSaveFileAsync(); // if (file == null) // { // PostMessage("SaveAFile", "Operation cancelled."); // rcvr = null; // return; // } // } // FileDetail rcvFile = await rcvr.ReadAsync(); // if (rcvFile == null) // { // PostMessage("SaveAFile", "Operation failed."); // rcvr = null; // return; // } // if (savePicker != null) // { // await file.RenameAsync(rcvFile.filename); // } // else // { // usedPicker = false; // Windows.Storage.StorageFolder storageFolder = // Windows.Storage.ApplicationData.Current.LocalFolder; // file = // await storageFolder.CreateFileAsync(rcvFile.filename, // Windows.Storage.CreationCollisionOption.ReplaceExisting); // } // if (file != null) // { // // Prevent updates to the remote version of the file until // // we finish making changes and call CompleteUpdatesAsync. // Windows.Storage.CachedFileManager.DeferUpdates(file); // // write to file // await Windows.Storage.FileIO.WriteTextAsync(file, rcvFile.txt); // // Let Windows know that we're finished changing the file so // // the other app can update the remote version of the file. // // Completing updates may require Windows to ask for user input. // Windows.Storage.Provider.FileUpdateStatus status = // await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); // if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) // { // PostMessage( "File " , file.Name + " was saved in \r\n" + file.Path); // } // else // { // PostMessage( "File " , file.Name + " couldn't be saved."); // } // } // rcvr = null; // } private async Task SaveAFile2(FileDetail rcvFile) { FileSavePicker savePicker = null; Windows.Storage.StorageFile file = null; savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("Plain Text", new List <string>() { ".txt" }); // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = rcvFile.filename; file = await savePicker.PickSaveFileAsync(); if (file == null) { PostMessage("SaveAFile", "Operation cancelled."); return; } //await file.RenameAsync(rcvFile.filename); // Prevent updates to the remote version of the file until // we finish making changes and call CompleteUpdatesAsync. Windows.Storage.CachedFileManager.DeferUpdates(file); // write to file await Windows.Storage.FileIO.WriteTextAsync(file, rcvFile.txt); // Let Windows know that we're finished changing the file so // the other app can update the remote version of the file. // Completing updates may require Windows to ask for user input. Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { PostMessage("File ", file.Name + " was saved in \r\n" + file.Path); } else { PostMessage("File ", file.Name + " couldn't be saved."); } }
public async Task ReadAsync(StreamSocket _socket) { FileDetail fi = new FileDetail(); Connected = true; MainPage.root.RecvConnected = true; using (DataReader reader = new DataReader(_socket.InputStream)) { try { //Read filename then file contents for (int i = 0; (i < 2); i++) { if (_socket == null) { Connected = false; } else if (_socket.InputStream == null) { Connected = false; } ; if (Connected) { // Based on the protocol we've defined, the first uint is the size of the message uint readLength = await reader.LoadAsync(sizeof(uint)); // Check if the size of the data is expected (otherwise the remote has already terminated the connection) if (readLength < sizeof(uint)) { //remoteDisconnection = true; Connected = false; } else { uint currentLength = reader.ReadUInt32(); // Load the rest of the message since you already know the length of the data expected. readLength = await reader.LoadAsync(currentLength); // Check if the size of the data is expected (otherwise the remote has already terminated the connection) if (readLength < currentLength) { Connected = false; } else { string message = reader.ReadString(currentLength); if (message == Constants.EndTransmission) { Connected = false; } else { if (i == 0) { fi.filename = message; } else { fi.txt = message; MainPage.root.SaveFile(fi); } } } } } } } // Catch exception HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED). catch (Exception ex) when((uint)ex.HResult == 0x800703E3) { PostMessage("OBEX_Receiver.ReadAsync", ex.Message); fi = null; Connected = false; } reader.DetachStream(); } MainPage.root.RecvConnected = false; if (_socket != null) { _socket.Dispose(); } Connected = false; }