#20893: Adding shortcut extension methods to IStorageProvider to simply read and write files.

Work Item: 20893
This commit is contained in:
Lombiq
2014-08-20 00:44:02 +02:00
committed by Zoltán Lehóczky
parent 12603a760d
commit cb98e31df1
2 changed files with 35 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Orchard.FileSystems.Media {
public static class StorageProviderExtensions {
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 string ReadAllText(this IStorageProvider storageProvider, string path) {
if (!storageProvider.FileExists(path)) {
return String.Empty;
}
var file = storageProvider.GetFile(path);
using (var stream = file.OpenRead())
using (var streamReader = new StreamReader(stream)) {
return streamReader.ReadToEnd();
}
}
}
}

View File

@@ -259,6 +259,7 @@
<Compile Include="FileSystems\Media\ConfigurationMimeTypeProvider.cs" />
<Compile Include="FileSystems\Media\FileSystemStorageProvider.cs" />
<Compile Include="FileSystems\Media\IMimeTypeProvider.cs" />
<Compile Include="FileSystems\Media\StorageProviderExtensions.cs" />
<Compile Include="Indexing\ISearchBits.cs" />
<Compile Include="Localization\Services\CurrentCalendarWorkContext.cs" />
<Compile Include="Localization\Services\CurrentCultureWorkContext.cs" />