public static byte[] ReadFileToByte(string fileName)
{
byte[] data = null;
// Check whether the image file exists or not
if (fileName == null || fileName.Length <= 0 || !File.Exists(fileName))
{
return data;
}
try
{
// Open the image file
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
// Read the image into the byte variable
data = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
}
catch(Exception ex)
{
...
}
return data;
}
/// <summary>
/// Write the content of a byte variable into a file
/// </summary>
/// <param name="data">The byte variable</param>
/// <param name="fileName">The name of the target file</param>
public static void WriteByteToFile(byte[] data, string fileName)
{
// Check whether there is data in the byte variable
if ((data==null) || (data.Length<=0))
{
return;
}
if (fileName==null||fileName.Trim()==String.Empty)
{
return;
}
try
{
// Open the target file. If it doesn't exist, create it.
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
// Write the content of the byte variable into the file
bw.Write(data);
bw.Flush();
bw.Close();
fs.Close();
}
catch(Exception ex)
{
...
}
}
Monday, October 1, 2007
How to read the content of a file into Byte and write Byte to a file?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment