public DirectoryInfo(string path) { if (path == null) { throw new ArgumentNullException(); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException(); } try { this.path = path; this.folder = FileHelper.GetFolderForPathOrURI(path); } catch (IOException ex) { System.Diagnostics.Debug.WriteLine("DirectoryInfo: " + ex.Message + "\n" + ex.StackTrace); throw; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("DirectoryInfo: " + ex.Message + "\n" + ex.StackTrace); throw new IOException(ex.Message, ex); } }
private static StorageFile CreateOrReplaceFile(string path) { IAsyncOperation <StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.ReplaceExisting); WindowsRuntimeSystemExtensions.AsTask <StorageFile>(fileAsync).Wait(); return(fileAsync.GetResults()); }
private static StorageFile CreateFile(string file, FileMode mode, FileAccess access) { IAsyncOperation <StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(file).CreateFileAsync(Path.GetFileName(file), FileStream.GetCollisionOption(mode, access)); WindowsRuntimeSystemExtensions.AsTask <StorageFile>(fileAsync).Wait(); if (fileAsync.Status != AsyncStatus.Completed) { System.Diagnostics.Debug.WriteLine("FileStream.CheckAccess: Failed to create file " + file); throw new IOException("Failed to create file " + file); } else { return(fileAsync.GetResults()); } }
internal override void RefreshInternal() { try { this.folder = FileHelper.GetFolderForPathOrURI(this.path); } catch (IOException ex) { System.Diagnostics.Debug.WriteLine("DirectoryInfo.RefreshInternal: " + ex.Message + "\n" + ex.StackTrace); throw; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("DirectoryInfo.RefreshInternal: " + ex.Message + "\n" + ex.StackTrace); throw new IOException(ex.Message, ex); } }
public static StreamWriter AppendText(string path, Encoding encoding) { try { IAsyncOperation <StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.OpenIfExists); WindowsRuntimeSystemExtensions.AsTask <StorageFile>(fileAsync).Wait(); IAsyncOperation <IRandomAccessStream> source = fileAsync.GetResults().OpenAsync(FileAccessMode.ReadWrite); WindowsRuntimeSystemExtensions.AsTask <IRandomAccessStream>(source).Wait(); FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults(); randomAccessStream.Seek(randomAccessStream.Size); return(encoding == null ? new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream)) : new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream), encoding)); } catch (Exception ex) { throw File.GetRethrowException(ex); } }
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName) { if (destinationFileName == null) { throw new ArgumentNullException(); } if (!string.IsNullOrWhiteSpace(sourceFileName) && !string.IsNullOrWhiteSpace(destinationFileName)) { if (!string.IsNullOrWhiteSpace(destinationBackupFileName)) { try { StorageFile fileForPathOrUri1 = FileHelper.GetFileForPathOrURI(sourceFileName); string fileName1 = Path.GetFileName(destinationFileName); StorageFile fileForPathOrUri2; if (fileName1.ToLower() == destinationFileName.ToLower()) { if (Path.GetFileName(sourceFileName).ToLower() == fileName1.ToLower()) { System.Diagnostics.Debug.WriteLine("File.Replace: Source and destination is the same file"); throw new IOException("Source and destination is the same file"); } fileForPathOrUri2 = FileHelper.GetFileForPathOrURI(Path.Combine(Path.GetDirectoryName(fileForPathOrUri1.Path), fileName1)); } else { fileForPathOrUri2 = FileHelper.GetFileForPathOrURI(destinationFileName); if (fileForPathOrUri1.Equals((object)fileForPathOrUri2)) { System.Diagnostics.Debug.WriteLine("File.Replace: Source and destination is the same file"); throw new IOException("Source and destination is the same file"); } } string fileName2 = Path.GetFileName(destinationBackupFileName); if (fileName2.ToLower() == destinationBackupFileName.ToLower()) { WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri2.RenameAsync(destinationBackupFileName)).Wait(); } else { StorageFolder folderForPathOrUri = FileHelper.GetFolderForPathOrURI(destinationBackupFileName); WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri2.MoveAsync((IStorageFolder)folderForPathOrUri, fileName2)).Wait(); } if (fileName1.ToLower() == destinationFileName.ToLower()) { WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri1.RenameAsync(destinationFileName)).Wait(); return; } else { StorageFolder folderForPathOrUri = FileHelper.GetFolderForPathOrURI(destinationFileName); WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri1.MoveAsync((IStorageFolder)folderForPathOrUri, fileName1)).Wait(); return; } } catch (Exception ex) { throw File.GetRethrowException(ex); } } } throw new ArgumentException(); }
public static void Move(string sourceFileName, string destFileName) { try { WindowsRuntimeSystemExtensions.AsTask(FileHelper.GetFileForPathOrURI(sourceFileName).MoveAsync((IStorageFolder)FileHelper.GetFolderForPathOrURI(destFileName), Path.GetFileName(destFileName))).Wait(); } catch (Exception ex) { throw File.GetRethrowException(ex); } }
public static void Copy(string sourceFileName, string destFileName, bool overwrite) { try { StorageFile fileForPathOrUri = FileHelper.GetFileForPathOrURI(sourceFileName); if (overwrite) { StorageFile storageFile = (StorageFile)null; try { storageFile = FileHelper.GetFileForPathOrURI(destFileName); } catch { } if (storageFile != null) { WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri.CopyAndReplaceAsync((IStorageFile)storageFile)).Wait(); return; } } WindowsRuntimeSystemExtensions.AsTask <StorageFile>(fileForPathOrUri.CopyAsync((IStorageFolder)FileHelper.GetFolderForPathOrURI(destFileName), Path.GetFileName(destFileName))).Wait(); } catch (Exception ex) { throw File.GetRethrowException(ex); } }
public static Stream OpenFileForWriting(string uri) { string fileName = Path.GetFileName(uri); Task <StorageFile> task1 = WindowsRuntimeSystemExtensions.AsTask <StorageFile>(FileHelper.GetFolderForPathOrURI(uri).CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting)); task1.Wait(); if (task1.Status != TaskStatus.RanToCompletion) { throw new Exception("Failed to open the file"); } Task <IRandomAccessStream> task2 = WindowsRuntimeSystemExtensions.AsTask <IRandomAccessStream>(task1.Result.OpenAsync(FileAccessMode.ReadWrite)); task2.Wait(); if (task2.Status != TaskStatus.RanToCompletion) { throw new Exception("Failed to open the file"); } else { return(WindowsRuntimeStreamExtensions.AsStreamForWrite((IOutputStream)task2.Result)); } }