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;
}
bw.Close(); is missing.
ReplyDeleteOthewise works fine.
Karlo
Thanks Karlo, yes, not having that line could cause issues. I made the change.
ReplyDeleteHi,
ReplyDeletestring 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???
fs.close() missing and can be open in writeonly mode ;-)
ReplyDeleteAnyway, thanks for snippet
You rule buddy...thanks heaps
ReplyDeleteThanks a lot for this code.
ReplyDeleteRegards,
Rohit Toshniwal
Just use File.WriteAllBytes(string path, byte[] bytes);
ReplyDeleteStefan, I like you're work. Cheers.
ReplyDeleteThanks, Excellent code.
ReplyDeletethanks for the example, for more details
ReplyDeletehttp://msdn.microsoft.com/en-us/library/d62kzs03.aspx
this code gvve me exception
ReplyDeleteAccess 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 .
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.
ReplyDeleteThe 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).
thanks alot..It helped me
ReplyDeletethank u for providing this code
ReplyDeleteIn NetMF 4.1 the BinaryWriter doesn't seem to exist (have declared 'using Susyem.IO').
ReplyDeleteIs it something that's just left out of the cutdown framework?
Thank you very much for the extremely useful tutorial! It was extremely helpful.
ReplyDeleteYou wouldn't need a close() if you enclosed used a "using" block.
ReplyDelete