SORU
11 Kasım 2008, Salı


Nasıl bayt kısaltma kullanarak okunabilir bir dosya boyutu alabilirim .NET?

Nasıl bayt kısaltma kullanarak okunabilir bir dosya boyutu alabilirim .NET?

Örnek: Giriş 7,326,629 ve ekran 6.98 MB al

CEVAP
11 Kasım 2008, Salı


Bunu yapmanın en etkili yolu değil, ama eğer günlük matematik biliyorsanız değil, okumayı kolaylaştırmak için, ve çoğu senaryoları için yeterince hızlı olmalıdır.

string[] sizes = { "B", "KB", "MB", "GB" };
double len = new FileInfo(filename).Length;
int order = 0;
while (len >= 1024 && order   1 < sizes.Length) {
    order  ;
    len = len/1024;
}

// Adjust the format string to your preferences. For example "{0:0.#}{1}" would
// show a single decimal place, and no space.
string result = String.Format("{0:0.##} {1}", len, sizes[order]);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Chanre Joubert

    Chanre Joube

    27 Temmuz 2012
  • michellefeng's channel

    michellefeng

    26 Kasım 2006
  • TheJoeycool2010

    TheJoeycool2

    12 Temmuz 2010