mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
committed by
Sébastien Ros
parent
59dc2bab85
commit
2821e362a0
@@ -113,11 +113,34 @@ namespace Orchard.FileSystems.AppData {
|
|||||||
|
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
|
||||||
public void CreateFile(string path, string content) {
|
public void CreateFile(string path, string content) {
|
||||||
using (var stream = CreateFile(path)) {
|
var filePath = CombineToPhysicalPath(path);
|
||||||
using (var tw = new StreamWriter(stream)) {
|
var folderPath = Path.GetDirectoryName(filePath);
|
||||||
tw.Write(content);
|
if (!Directory.Exists(folderPath)) {
|
||||||
|
Directory.CreateDirectory(folderPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// for existing files, just overwrite
|
||||||
|
if (File.Exists(filePath)) {
|
||||||
|
File.WriteAllText(filePath, content);
|
||||||
|
} else {
|
||||||
|
WriteAllTextWithTempFileAndMove(filePath, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void WriteAllTextWithTempFileAndMove(string path, string content) {
|
||||||
|
// generate a temp file
|
||||||
|
var tempPath = Path.GetTempFileName();
|
||||||
|
|
||||||
|
// write the data to a temp file
|
||||||
|
using (var tempFile = File.Create(tempPath, 4096, FileOptions.WriteThrough)) {
|
||||||
|
using (var writer = new StreamWriter(tempFile)) {
|
||||||
|
writer.Write(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// move to destination
|
||||||
|
// will throw exception if destination exists
|
||||||
|
File.Move(tempPath, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stream CreateFile(string path) {
|
public Stream CreateFile(string path) {
|
||||||
|
|||||||
Reference in New Issue
Block a user