getting file bytesize
-
hi to all, i am trying to get the bytes size of a file so i can display in statusbar, also updated as typed. i first tried getting the encoding of the file and using getbytes() of the encoder, but it didnt come up the same as Windowx Explorer, then i tried just to get the char count and multiply * 2 but it still not work. how is this acheived ? this is my encoder attempt
string byteSuffix = "B"; // update file size panel string text = this.context.Text; if(text == null) text = ""; byte[] bytes = this.encoder.GetBytes(text); double size = bytes.LongLength; string adjustedSize = "0.0"; string byteSize = "0.0"; // gb if(size >= 1000000000) { byteSuffix = "GB"; byteSize = size + ""; size = size / Math.Pow(2, 30); adjustedSize = size.ToString("N2"); } // mb else if(size >= 1000000) { byteSuffix = "MB"; byteSize = size + ""; size = size / Math.Pow(2, 20); adjustedSize = size.ToString("N2"); } // kb else if(size >= 1000) { byteSuffix = "KB"; byteSize = size + ""; size = size / Math.Pow(2, 10); adjustedSize = size.ToString("N2"); } this.statusFileSize.Text = adjustedSize + " " + byteSuffix; this.statusFileSize.ToolTipText = adjustedSize + " " + byteSuffix + " (" + byteSize + " bytes)"
this is how i get encoderStreamReader reader = null; try { reader = new StreamReader(this.path, true); this.Content = reader.ReadToEnd(); this.encoder = reader.CurrentEncoding; this.Encoding = encoder.EncodingName; reader.Close(); // set default encoder if(this.encoder == null) { this.encoder = ASCIIEncoding.Unicode; this.Encoding = encoder.EncodingName; } } catch (Exception e) { Debug.WriteLine("Error: " + e.Message); } finally { if(reader != null) reader.Close(); }
kind regards, g00fy -
hi to all, i am trying to get the bytes size of a file so i can display in statusbar, also updated as typed. i first tried getting the encoding of the file and using getbytes() of the encoder, but it didnt come up the same as Windowx Explorer, then i tried just to get the char count and multiply * 2 but it still not work. how is this acheived ? this is my encoder attempt
string byteSuffix = "B"; // update file size panel string text = this.context.Text; if(text == null) text = ""; byte[] bytes = this.encoder.GetBytes(text); double size = bytes.LongLength; string adjustedSize = "0.0"; string byteSize = "0.0"; // gb if(size >= 1000000000) { byteSuffix = "GB"; byteSize = size + ""; size = size / Math.Pow(2, 30); adjustedSize = size.ToString("N2"); } // mb else if(size >= 1000000) { byteSuffix = "MB"; byteSize = size + ""; size = size / Math.Pow(2, 20); adjustedSize = size.ToString("N2"); } // kb else if(size >= 1000) { byteSuffix = "KB"; byteSize = size + ""; size = size / Math.Pow(2, 10); adjustedSize = size.ToString("N2"); } this.statusFileSize.Text = adjustedSize + " " + byteSuffix; this.statusFileSize.ToolTipText = adjustedSize + " " + byteSuffix + " (" + byteSize + " bytes)"
this is how i get encoderStreamReader reader = null; try { reader = new StreamReader(this.path, true); this.Content = reader.ReadToEnd(); this.encoder = reader.CurrentEncoding; this.Encoding = encoder.EncodingName; reader.Close(); // set default encoder if(this.encoder == null) { this.encoder = ASCIIEncoding.Unicode; this.Encoding = encoder.EncodingName; } } catch (Exception e) { Debug.WriteLine("Error: " + e.Message); } finally { if(reader != null) reader.Close(); }
kind regards, g00fyYou can try using the simple alternative of FileInfo.Length property to get the size of a file in bytes. If strMyFile represents the full name-path of the your file, then
FileInfo fiMyFile = new FileInfo(strMyFile);
long lLengthMyFile = fiMyFile.Length;This will give the size of your file in bytes. Use the "using System.IO;" at the beginning for accessing FileInfo class. Regards, Ashok Dhamija _____________________________ Padam Technologies
-
You can try using the simple alternative of FileInfo.Length property to get the size of a file in bytes. If strMyFile represents the full name-path of the your file, then
FileInfo fiMyFile = new FileInfo(strMyFile);
long lLengthMyFile = fiMyFile.Length;This will give the size of your file in bytes. Use the "using System.IO;" at the beginning for accessing FileInfo class. Regards, Ashok Dhamija _____________________________ Padam Technologies
-
is there another way though, this is fine when i open a file, but i would like to update the size as i type into the file to use this method i would have to save file after each keystroke. kind regards, g00fy
Try using
Refresh()
method ofFileInfo
class before calling theLength
property every time you want to update it, i.e.,fiMyFile.Refresh();
lLengthMyFile = fiMyFile.Length;Though I have not checked it in the context of your application (but it worked in my application having some different requirements), it may work if the character is being written in the file as it is being typed. Another way out could be to use the
Length
property ofFileInfo
as above as and when you save the file. And, after that as the characters are being typed, you keep a count of the characters / bytes of the unsaved data and add it to theLength
calculated earlier for the saved data. I hope it works. Regards, Ashok Dhamija _____________________________ Padam Technologies -- modified at 1:13 Monday 19th September, 2005