using (var writer = File.CreateText(fullFilePath))
{
file.Write(fileContent);
}
给定上面的代码,文件大小可以从StreamWriter
?
using (var writer = File.CreateText(fullFilePath))
{
file.Write(fileContent);
}
给定上面的代码,文件大小可以从StreamWriter
?
是的,你可以,试试下面的
long length = writer.BaseStream.Length;//will give unexpected output if autoflush is false and write has been called just before
注意:writer.BaseStream.Length
属性可能会返回意外结果,因为StreamWriter
不会立即写入。它缓存以便获得您需要的预期输出AutoFlush = true
writer.AutoFlush = true; or writer.Flush();
long length = writer.BaseStream.Length;//will give expected output
如果您需要特定文件的属性,我认为您正在寻找的是 FileInfo。
FileInfo info = new FileInfo(fullFilePath);
//Gets the size, in bytes, of the current file.
long size = info.Length;
干得好!只需使用 System.IO 命名空间中的 FileInfo 类 :)
using System.IO;
var fullFilePath = "C:\path\to\some\file.txt";
var fileInfo = new FileInfo(fullFilePath);
Console.WriteLine("The size of the file is: " + fileInfo.Length);
你能试试这段代码吗?
var size = writer.BaseStream.Length;
*writer 是你的 StreamWriter
不,不是从 StreamWriter 本身,您可以了解文件的大小。您必须使用FileInfo 的 Length。