Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. getting file bytesize

getting file bytesize

Scheduled Pinned Locked Moved C#
debugginghelpquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    g00fyman
    wrote on last edited by
    #1

    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 encoder StreamReader 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

    A 1 Reply Last reply
    0
    • G g00fyman

      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 encoder StreamReader 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

      A Offline
      A Offline
      Ashok Dhamija
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • A Ashok Dhamija

        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

        G Offline
        G Offline
        g00fyman
        wrote on last edited by
        #3

        easy when you know how eh :) just hard to google for some things thank you g00fy

        G 1 Reply Last reply
        0
        • G g00fyman

          easy when you know how eh :) just hard to google for some things thank you g00fy

          G Offline
          G Offline
          g00fyman
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • G g00fyman

            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

            A Offline
            A Offline
            Ashok Dhamija
            wrote on last edited by
            #5

            Try using Refresh() method of FileInfo class before calling the Length 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 of FileInfo 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 the Length calculated earlier for the saved data. I hope it works. Regards, Ashok Dhamija _____________________________ Padam Technologies -- modified at 1:13 Monday 19th September, 2005

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups