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
Flush()
。 - H HFileInfo 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);
using(){}
上下文中显示。 - H Husing(writer)
之外,且在它之后是有必要的。 - H H你能试试这段代码吗?
var size = writer.BaseStream.Length;
*writer 是你的 StreamWriter
writer.Write(fileContent);
- Moh Moh Oo