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. File to byte[ ]

File to byte[ ]

Scheduled Pinned Locked Moved C#
question
3 Posts 3 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.
  • W Offline
    W Offline
    WDI
    wrote on last edited by
    #1

    and populate it with file. what should i do for File to byte[ ] ? thanks.

    W 1 Reply Last reply
    0
    • W WDI

      and populate it with file. what should i do for File to byte[ ] ? thanks.

      W Offline
      W Offline
      Wender Oliveira
      wrote on last edited by
      #2

      just change your max file size and filepath... const int MaxSize = 1000; string FilePath = @"C:\test.txt"; byte[] b = new byte[MaxSize]; FileStream f = new FileStream(FilePath,FileMode.Open); f.Read(b,0,(int)f.Length); Wender Oliveira .NET Programmer

      D 1 Reply Last reply
      0
      • W Wender Oliveira

        just change your max file size and filepath... const int MaxSize = 1000; string FilePath = @"C:\test.txt"; byte[] b = new byte[MaxSize]; FileStream f = new FileStream(FilePath,FileMode.Open); f.Read(b,0,(int)f.Length); Wender Oliveira .NET Programmer

        D Offline
        D Offline
        Dennis C Dietrich
        wrote on last edited by
        #3

        Wender Oliveira wrote: just change your max file size and filepath... const int MaxSize = 1000; string FilePath = @"C:\test.txt"; byte[] b = new byte[MaxSize]; FileStream f = new FileStream(FilePath,FileMode.Open); f.Read(b,0,(int)f.Length); No. I've seen it many times and it is just wrong. You end up with an array with then length 1000. How does that reflect the content of the array? It doesn't. The object processing the array has to analyze the data in order to find out the length of the actual user data (sometimes that might even be impossible). And what if MaxSize is much larger? Just read the entire file as originally asked for and if you want to introduce a limit then use something like this (and make sure the receiving object is aware of the fact that the data might be truncated):

        byte[] MyArray = null;
        const int MaxSize = 1000;
        string FilePath = @"C:\test.txt";
        FileInfo MyFile = new FileInfo(FilePath);

        if (MyFile.Length > MaxSize)
        MyArray = new byte[MaxSize];
        else
        MyArray = new byte[MyFile.Length];

        Best regards Dennis

        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