Monday, November 2, 2009

Converting any File to Byte[] and Byte[] to File.

Below C# code takes filepath, where file located as input parameter and gives byte array of that file.

public byte[] ReadFile(string sPath)
{
FileStream fStream = null;
try
{byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
using (fStream)
{
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
}
return data;
}
catch { throw; }
finally { fStream.Dispose(); }
}


Below code snippets takes Byte array and File extention as input parameter and stores the file in application path under temp folder.

public byte[] ReadFile(string sPath)
{
FileStream fStream = null;
try
{byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
using (fStream)
{
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
}
return data;
}
catch { throw; }
finally { fStream.Dispose(); }
}

Sunday, November 1, 2009

Considering Comments in ASP.Net webpage.

“Even small thing make a big difference”, to prove this, commenting content in ASP.Net webpage page can be done using two methods.


Important and considerable difference between these two comments were, with HTML comments, content commented get rendered when web page requested and result in increasing download size. So its good to use ASP.NET comments.