System.IO.File.GetLength gives me the size of a file in bytes. What I'm attempting to do is take that size, in bytes, and convert it into the highest unit possible, without ever letting the size become less than 1. For example, instead of "2048" showing in the size column, I want "2 KB" to show. Here's the code:
private string sizeDown(string size) { decimal sizeoffile = Convert.ToDecimal(size); int downed = 0; do { sizeoffile = sizeoffile / 1024; downed += 1; } while (sizeoffile > 1024); if (downed > 3) { return ""; } else { switch (downed) { case 0: return Convert.ToString(sizeoffile) + " bytes"; break; case 1: return Convert.ToString(sizeoffile) + " KB"; break; case 2: return Convert.ToString(sizeoffile) + " MB"; break; case 3: return Convert.ToString(sizeoffile) + " GB"; } } } However, as you probably see from the title, Visual Studio tells me that not all code paths return a value. I'm pretty confused. What value of size could result in my code not returning a value?
(By the way, I am aware that I take size as a string and then convert it into a decimal - I do that for convenience.)
downedis negative?if(downed > 3)and add it asdefaultto theswitchstatement.dokeyword and replace it with the while. Or see my example.