【发布时间】:2014-05-13 18:05:48
【问题描述】:
大家好,我有一个 DataTable,它保存文件名,路径如下
String[] sizeArry = new String[] { "Byes", "KB", "MB" };
String GetSize(ulong sizebytes, int index)
{
if (sizebytes < 1000) return sizebytes + sizeArry[index];
else return GetSize(sizebytes / 1024, ++index);
}
protected DataTable GetAttachment()
{
DataTable attachment = new DataTable();
attachment.Columns.Add("ID", typeof(int));
attachment.Columns.Add("Attachment", typeof(string));
attachment.Columns.Add("Size", typeof(string));
attachment.Rows.Add(1, " files/abc.txt");
attachment.Rows.Add(2, "files/test.pdf");
foreach (DataRow row in attachment.Rows)
{
string sFilename = row["Attachment"].ToString();
string path = Server.MapPath(sFilename);
FileInfo fi = new FileInfo(path);
row["Attachment"] = fi.Name;
row["Size"] = GetSize((ulong)fi.Length, 0);
}
Total(attachment);
return attachment;
}
这给我的结果如下abc.txt 3KB test.pdf 11MB 现在我需要总结这些尺寸并需要在label 中显示尺寸所以有人可以帮助我。我尝试如下,但没有按要求工作
protected void Total(DataTable dt)
{
int size = 0;
int cnt = 0;
foreach (DataRow row in dt.Rows)
{
cnt++;
string s = row["Size"].ToString().Remove(row["Size"].ToString().Length - 2);
size = size + Convert.ToInt16(s);
}
label.Text = cnt.ToString() + " attachments " + size.ToString();
}
【问题讨论】:
-
这是什么意思,它没有按要求工作?总和错了?还是?
-
是的 sum 是错误的,它显示
14mb这是错误的