12
using (var writer = File.CreateText(fullFilePath))
{
   file.Write(fileContent);
}

给定上面的代码,文件大小可以从StreamWriter?

4

5 回答 5

26

是的,你可以,试试下面的

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
于 2013-08-08T13:43:20.550 回答
3

如果您需要特定文件的属性,我认为您正在寻找的是 FileInfo。

FileInfo info = new FileInfo(fullFilePath);
//Gets the size, in bytes, of the current file.
long size = info.Length;
于 2013-08-08T13:41:49.983 回答
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);
于 2013-08-08T13:41:37.823 回答
0

你能试试这段代码吗?

var size = writer.BaseStream.Length;

*writer 是你的 StreamWriter

于 2013-08-08T13:42:48.200 回答
0

不,不是从 StreamWriter 本身,您可以了解文件的大小。您必须使用FileInfo 的 Length

于 2013-08-08T13:45:17.337 回答