Hack Forums
[C#] [C#] How to write and read bytes ? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [C#] How to write and read bytes ? (/showthread.php?tid=1542221)



[C#] How to write and read bytes ? - sn4pcr4k - Jul 30, 2011

I've got byte[] lol = bunch of stuff
Length: {byte[64]}

saving it to conf.conf using:
Code
public static void writebytes(byte[] bytes, string path)
    {
  using (BinaryWriter binWriter =
  new BinaryWriter(File.Open(path, FileMode.Create)))
  {
  binWriter.Write(bytes);
  }
    }

Then I load it in using:
Code
public static byte[] Read(string path)
    {
  try { return File.ReadAllBytes(path); }
  catch { return null; }
    }

And then the Length is {byte[108]}

wtF? Is that because the filename is also in there? How i retreive the stuff i just saved ? !


* - Black Hat Jack - Jul 30, 2011

(Jul 30, 2011 02:47 PM)sn4pcr4k Wrote:  I've got byte[] lol = bunch of stuff
Length: {byte[64]}

saving it to conf.conf using:
Code
public static void writebytes(byte[] bytes, string path)
    {
  using (BinaryWriter binWriter =
  new BinaryWriter(File.Open(path, FileMode.Create)))
  {
  binWriter.Write(bytes);
  }
    }

Then I load it in using:
Code
public static byte[] Read(string path)
    {
  try { return File.ReadAllBytes(path); }
  catch { return null; }
    }

And then the Length is {byte[108]}

wtF? Is that because the filename is also in there? How i retreive the stuff i just saved ? !

How are you trying to use it?



* - hgdfhsdgdfgdfh - Jul 30, 2011

Try adding binWriter.Flush(); after you have written your data.


* - sn4pcr4k - Jul 30, 2011

Thanks for the help, made a very stupid mistake. I should look at my code longer next time.