Saturday, December 29, 2007

C#: Write a byte array to a file

public bool writeByteArrayToFile(byte[] buff, string fileName)
{
bool response = false;

try
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff);
bw.Close(); //Thanks Karlo for pointing out!
response = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

return response;
}

17 comments:

  1. bw.Close(); is missing.

    Othewise works fine.

    Karlo

    ReplyDelete
  2. Thanks Karlo, yes, not having that line could cause issues. I made the change.

    ReplyDelete
  3. Hi,

    string abcdef = 979899100101102 when converted into bytes

    string xyz = 120121122 when converted in to bytes

    SENARIO:

    string str1 = "abcdef";

    when converted into bytes it becomes: 979899100101102

    i want to modify these bytes and then enter them AS BYTES in to a new .txt file so that when that txt file is opened it has "xyz" in it.


    is it possible???

    ReplyDelete
  4. fs.close() missing and can be open in writeonly mode ;-)

    Anyway, thanks for snippet

    ReplyDelete
  5. Thanks a lot for this code.

    Regards,
    Rohit Toshniwal

    ReplyDelete
  6. Just use File.WriteAllBytes(string path, byte[] bytes);

    ReplyDelete
  7. Stefan, I like you're work. Cheers.

    ReplyDelete
  8. thanks for the example, for more details
    http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

    ReplyDelete
  9. this code gvve me exception

    Access to the path 'd:\sdsds.png' is denied.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.UnauthorizedAccessException: Access to the path 'd:\sdsds.png' is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

    To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.


    what to do foe this please help .

    ReplyDelete
  10. Vijay - if you want to write files using ASP.NET without modifying permissions then use the App_Data directory - that's what it is there for.

    The App_Data directory can be written to and read from using code on the web site, but it won't be exposed to the outside world (unless your code publishes the content).

    ReplyDelete
  11. thank u for providing this code

    ReplyDelete
  12. In NetMF 4.1 the BinaryWriter doesn't seem to exist (have declared 'using Susyem.IO').

    Is it something that's just left out of the cutdown framework?

    ReplyDelete
  13. Thank you very much for the extremely useful tutorial! It was extremely helpful.

    ReplyDelete
  14. You wouldn't need a close() if you enclosed used a "using" block.

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.