Browse Source

修正 WriteFile 不清空文件的问题。

master
Elivo 2 weeks ago
parent
commit
645dd892a7
  1. 26
      Apewer/StorageUtility.cs

26
Apewer/StorageUtility.cs

@ -311,7 +311,7 @@ namespace Apewer
#region file #region file
/// <summary>打开文件,并获取文件流。若文件不存在,则先创建文件;若获取失败,则返回 NULL 值。可选文件的锁定状态。</summary>
/// <summary>打开文件,并获取文件流。若文件不存在,则先创建文件。</summary>
public static FileStream OpenFile(string path, bool share = true) public static FileStream OpenFile(string path, bool share = true)
{ {
try try
@ -450,27 +450,19 @@ namespace Apewer
} }
else else
{ {
var file = OpenFile(path, true);
var success = false;
try
{
var dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var write1 = BytesUtility.Write(file, TextUtility.Bom);
if (write1 == TextUtility.Bom.Length)
using (var file = OpenFile(path, true))
{ {
var write2 = BytesUtility.Write(file, bytes);
if (bytes != null && bytes.LongLength > 0L)
try
{ {
file.SetLength(0);
BytesUtility.Write(file, TextUtility.Bom);
BytesUtility.Write(file, bytes);
return true;
} }
success = true;
catch {
return false;
} }
} }
catch { }
RuntimeUtility.Dispose(file);
return success;
} }
} }
} }

Loading…
Cancel
Save