mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#20893: Adding shortcut extension methods to IStorageProvider to simply read and write files.
Work Item: 20893
This commit is contained in:
34
src/Orchard/FileSystems/Media/StorageProviderExtensions.cs
Normal file
34
src/Orchard/FileSystems/Media/StorageProviderExtensions.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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" />
|
||||
|
Reference in New Issue
Block a user