mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-19 09:42:29 +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")]
|
||||
public void CreateFile(string path, string content) {
|
||||
using (var stream = CreateFile(path)) {
|
||||
using (var tw = new StreamWriter(stream)) {
|
||||
tw.Write(content);
|
||||
var filePath = CombineToPhysicalPath(path);
|
||||
var folderPath = Path.GetDirectoryName(filePath);
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user