public static IStorageFile OpenOrCreate(this IStorageProvider storageProvider, string path) { if (!storageProvider.FileExists(path)) { return storageProvider.CreateFile(path); } return storageProvider.GetFile(path); }
public static void CopyBinaryFile(this IsolatedStorageFile isf, string source, string target, bool replace = false) { if (!isf.FileExists(target) || replace == true) { BinaryReader fileReader = new BinaryReader(GetFileStream(source)); if (GetDirectory(target) != string.Empty) IsolatedStorageFile.GetUserStoreForApplication().CreateDirectory(GetDirectory(target)); IsolatedStorageFileStream outFile = isf.CreateFile(target); bool eof = false; long fileLength = fileReader.BaseStream.Length; int writeLength = 512; while (!eof) { if (fileLength < 512) { writeLength = Convert.ToInt32(fileLength); outFile.Write(fileReader.ReadBytes(writeLength), 0, writeLength); } else { outFile.Write(fileReader.ReadBytes(writeLength), 0, writeLength); } fileLength = fileLength - 512; if (fileLength <= 0) eof = true; } fileReader.Close(); outFile.Close(); } }
/// <summary> /// Simple wrapper to create the given file (and close the handle) /// </summary> public static void CreateTestFile(this IsolatedStorageFile isf, string fileName, string content = null) { using (var stream = isf.CreateFile(fileName)) { if (content != null) stream.WriteAllText(content); } }
public static void CopyFile(this IsolatedStorageFile store, string source, string target) { using (var src = store.OpenFile(source, FileMode.Open)) { using (var dst = store.CreateFile(target)) { src.CopyTo(dst); } } }
public static void WriteAllText(this IStorageProvider storageProvider, string path, string contents) { if (storageProvider.FileExists(path)) { storageProvider.DeleteFile(path); } var file = storageProvider.CreateFile(path); using (var stream = file.OpenWrite()) using (var streamWriter = new StreamWriter(stream)) { streamWriter.Write(contents); } }
public static IFile CreateFileFromText(this IDirectory directory, string name, string text) { if (directory == null) { throw new ArgumentNullException("directory"); } var file = directory.CreateFile(name); using (StreamWriter writer = new StreamWriter(file.StreamProvider.OpenWrite())) { writer.Write(text); } return file; }
/// <summary> /// Copies a file. /// </summary> /// <param name="fileSystem">The file system</param> /// <param name="oldFilePath">The file that should be copied</param> /// <param name="newFilePath">The file that should be created</param> /// <param name="overwrite">If this parameter is true and created file will overwrite an existing file if one existing /// otherwise an error will be thrown.</param> public static void Copy(this IFileSystem fileSystem, FileReference oldFilePath, FileReference newFilePath, bool overwrite) { using (Stream oldStream = fileSystem.OpenFile(oldFilePath)) { if (oldStream == null) throw new System.IO.FileNotFoundException("File specified by oldFilePath parameter not found"); if (!overwrite && fileSystem.FileExists(newFilePath)) throw new System.IO.IOException("File specified by newFilePath parameter already exists."); using (Stream newStream = fileSystem.CreateFile(newFilePath)) { byte[] buffer = new byte[256]; int numRead; while ((numRead = oldStream.Read(buffer, 0, 256)) != 0) { newStream.Write(buffer, 0, numRead); } } } }
public static void CopyTextFile(this IsolatedStorageFile isf, string filename, bool replace = false) { if (!isf.FileExists(filename) || replace == true) { StreamResourceInfo sr = Application.GetResourceStream(new Uri(filename, UriKind.Relative)); if (sr != null) { using (StreamReader stream = new StreamReader(sr.Stream)) { IsolatedStorageFileStream outFile = isf.CreateFile(filename); string fileAsString = stream.ReadToEnd(); byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(fileAsString); outFile.Write(fileBytes, 0, fileBytes.Length); stream.Close(); outFile.Close(); } } } }
public static IOFileEntityWriter CreateFile( this IIOSystem system, string identifier) { return system.CreateFile(identifier, Encoding.Default); }
/// <summary> /// This will return a file location whether or not it exists /// </summary> /// <param name="directory"></param> /// <param name="fileName"></param> /// <returns></returns> public static IFile GetFile(this IDirectory directory, string fileName) { return directory.GetExistingFile(fileName) ?? directory.CreateFile(fileName); }
public static Task<IFile> CreateFile (this IFileSystem fs, string path, string contents) { var bytes = System.Text.Encoding.UTF8.GetBytes (contents); return fs.CreateFile (path, bytes); }
internal static void AddFile(this IFileSystem fileSystem, string path, Action<Stream> write) { using (var stream = fileSystem.CreateFile(path)) { write(stream); } }
public static IVirtualFile CopyFile(this IVirtualDirectory directory, IVirtualFile file, string name) { // TODO: use stream to stream copy (need AddFile out stream) var contents = file.ReadAllBytes(); return directory.CreateFile(name, contents); }
public static void CreateFile(this IFolder folder, string path, string content) { folder.ArgumentNullExceptionByNullOrEmpty("folder"); using (var stream = folder.CreateFile(path)) { using (var tw = new StreamWriter(stream)) { tw.Write(content); } } }