我想獲得在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