using (var writer = File.CreateText(fullFilePath))
{
file.Write(fileContent);
}
鉴于上述代码,可以从StreamWriter
获知文件大小吗?
答案 0 :(得分:17)
是的,您可以尝试以下
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
答案 1 :(得分:2)
如果您需要特定文件的属性,我认为您正在寻找的是FileInfo。
FileInfo info = new FileInfo(fullFilePath);
//Gets the size, in bytes, of the current file.
long size = info.Length;
答案 2 :(得分:1)
你走了!只需使用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);
答案 3 :(得分:0)
你可以试试这段代码吗?
var size = writer.BaseStream.Length;
* writer是您的StreamWriter
答案 4 :(得分:0)
不,不是从StreamWriter本身可以找到文件的大小。您必须使用FileInfo's Length。