如何在C#中计算文件大小

时间:2012-07-30 13:14:00

标签: asp.net-mvc-3 c#-4.0

在我的asp mvc 3应用程序中,我有一个允许用户下载给定文件的操作。

以下是代码:

public FilePathResult DownloadFile(string fileName)
    {
        try
        {
            string uploadsDocumentPath = System.Configuration.ConfigurationManager.AppSettings["uploadsDocumentPath"].ToString();
            string ext = Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); // henter info fra windows registry
            if (regKey != null && regKey.GetValue("Content Type") != null)
            {
                mimeType = regKey.GetValue("Content Type").ToString();
            }

            return File(uploadsDocumentPath + fileName, mimeType, fileName);
        }
        catch (Exception)
        {

            throw;
        }
    }

我希望只能下载大小小于150MB的文件。但我找不到如何计算这种类型的文件大小。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我想这应该有效:

FileInfo file = new FileInfo(uploadsDocumentPath + fileName);
if(file.Length > 157286400)
{
      // Return error here.
}