Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how i can copy file by stream file to byte array
and copy byte array to stream file and save this file
thanks for any help
Posted

Load your text file into a single string, and then call this method:

C#
//--------------------------------------------------------------------------------
public static byte[] StringToByteArray(string text)
{
    UTF8Encoding encoding = new UTF8Encoding();
    return encoding.GetBytes(text);
}


To put it back into a string, call this method:

C#
//--------------------------------------------------------------------------------
public static string ByteArrayToString(byte[] array)
{
    UTF8Encoding encoding = new UTF8Encoding();
    string text  = encoding.GetString(array);
    return text;
}
 
Share this answer
 
v4
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;
}

For save file, see this link
Click
 
Share this answer
 
Comments
Mostafa Elsadany 5-Feb-11 3:04am    
byte[] s=new byte [10050];
SaveFileDialog sv = new SaveFileDialog();
OpenFileDialog op = new OpenFileDialog();
string opfile, svfile;
if (op.ShowDialog() == DialogResult.OK && sv.ShowDialog() == DialogResult.OK)
{
opfile = op.FileName;
svfile = sv.FileName + Path .GetExtension (opfile);
MessageBox.Show(svfile );
bool x= writeByteArrayToFile(s, opfile );
File.WriteAllBytes(svfile, s);
MessageBox .Show (x.ToString ());
}
i try but when copy this byte array to any file give me error in file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900