public async Task RenameAsync(string newName) { Debug.WriteLine("WinRTStorageFile.cs | RenameAsync | "); await _baseFile.RenameAsync(newName, NameCollisionOption.ReplaceExisting); _renameTaskCompletionSource.SetResult(true); }
internal override async Task EnqueueAsync(Transmission transmission) { try { if (transmission == null || this.StorageFolder == null) { return; } // Initial storage size calculation. await this.EnsureSizeIsCalculatedAsync().ConfigureAwait(false); if ((ulong)this.storageSize >= this.CapacityInBytes || this.storageCountFiles >= this.MaxFiles) { // if max storage capacity has reached, drop the transmission (but log every 100 lost transmissions). if (this.transmissionsDropped++ % 100 == 0) { CoreEventSource.Log.LogVerbose("Total transmissions dropped: " + this.transmissionsDropped); } return; } // Writes content to a temporaty file and only then rename to avoid the Peek from reading the file before it is being written. // Creates the temp file name string tempFileName = Guid.NewGuid().ToString("N"); // Creates the temp file (doesn't save any content. Just creates the file) IStorageFile temporaryFile = await this.StorageFolder.CreateFileAsync(tempFileName + ".tmp").AsTask().ConfigureAwait(false); // Now that the file got created we can increase the files count Interlocked.Increment(ref this.storageCountFiles); // Saves transmission to the temp file await SaveTransmissionToFileAsync(transmission, temporaryFile).ConfigureAwait(false); // Now that the file is written increase storage size. long temporaryFileSize = await this.GetSizeAsync(temporaryFile).ConfigureAwait(false); Interlocked.Add(ref this.storageSize, temporaryFileSize); // Creates a new file name string now = DateTime.UtcNow.ToString("yyyyMMddHHmmss"); string newFileName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}.trn", now, tempFileName); // Renames the file await temporaryFile.RenameAsync(newFileName, NameCollisionOption.FailIfExists).AsTask().ConfigureAwait(false); } catch (Exception e) { CoreEventSource.Log.LogVerbose(string.Format(CultureInfo.InvariantCulture, "EnqueueAsync: Exception: {0}", e)); } }
/// <summary> /// Renames a file without changing its location. /// </summary> /// <param name="newName">The new leaf name of the file.</param> /// <param name="collisionOption">How to deal with collisions with existing files.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns> /// A task which will complete after the file is renamed. /// </returns> public async Task RenameAsync(string newName, NameCollisionOption collisionOption, CancellationToken cancellationToken) { Requires.NotNullOrEmpty(newName, "newName"); try { await _wrappedFile.RenameAsync(newName, (Windows.Storage.NameCollisionOption) collisionOption).AsTask(cancellationToken).ConfigureAwait(false); } catch (Exception ex) { if (ex.HResult == FILE_ALREADY_EXISTS) { throw new IOException("File already exists.", ex); } throw; } }
public async Task Flush() { if (!isEnabled) { return; } if (!isInit) { return; } bool earlyExit = false; lock (editLock) { earlyExit = isFlushInProgress; reFlush = isFlushInProgress; isFlushInProgress = true; } if (earlyExit) { return; } List <string> copy = new List <string>(); lock (listLock) { copy = new List <string>(buffer); buffer.Clear(); } StorageFolder localFolder = ApplicationData.Current.LocalFolder; bool spoolFile = false; if (copy.Count > 0) { try { using (var fileStream = await localFolder.OpenStreamForWriteAsync(FILENAME, CreationCollisionOption.OpenIfExists)) { foreach (string logMessage in copy) { byte[] bytes = Encoding.UTF8.GetBytes(logMessage.ToString()); fileStream.Write(bytes, 0, bytes.Length); if (fileStream.Length > MAX_FILE_LENGTH) { spoolFile = true; } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); Debugger.Break(); } } if (spoolFile) { IStorageFile currentLogFile = await localFolder.GetFileAsync(FILENAME); if (currentLogFile != null) { await currentLogFile.RenameAsync(SPOOLED_FILENAME, NameCollisionOption.ReplaceExisting); } } bool runAgain = false; lock (editLock) { isFlushInProgress = false; runAgain = reFlush; reFlush = false; } if (runAgain) { await Flush(); } }