我想获得在ListView中显示的大小。 但我的代码返回0.我find.Length()获取长度大小,但只有Count(),它不能解决我的问题。如何获取FileInfo中文件的大小?
我的代码,如:
string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
List<FileInfo> fileInfos = new List<FileInfo>();
foreach (string filePath in filePaths)
{
FileInfo f = new FileInfo(filePath);
fileInfos.Add(f);
long s1 = fileInfos.Count;
string chkExt = Path.GetExtension(f.ToString());
s1 = s1/1024;
decimal d = default(decimal);
d = decimal.Round(s1, 2, MidpointRounding.AwayFromZero);
// d is 0. Because s1 = 1 only count not length of file.
使用长度。也是整数除法; make 1024.0 –
'FileInfo'具有'Length'属性。它返回文件的字节数。 – daniel59
'fileInfos.Count'给出您读取的文件信息的数量。因此,对于第1024个文件的文件,它将返回0.在那之后,1等。您在那里使用了错误的编号。您应该在'f'中查找长度或大小属性,这是当前文件的实际文件信息。也许可以改进命名,这样可以降低再次发生此类错误的风险。 – GolezTrol