Thursday, 13 March 2014

How to convert file document to memorystream using C#, DotNet


Convert file document to memory stream using C#, DotNet

#region "Description"
/*       
   Use method given in post to convert file to MemoryStream
 
*/      
#endregion


#region "Method"
/// <summary>
/// Converts file to memorystream
/// </summary>
/// <param name="filePath">document file path</param>
/// <returns></returns>
 public System.IO.MemoryStream ConvertFileToMemoryStream(string filePath)
{
    try
    {
        System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

        fileStream.Position = 0;
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
        memoryStream.SetLength(fileStream.Length);
        fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);

        memoryStream.Flush();
        fileStream.Close();
        //memoryStream.Close();
        return memoryStream;
    }
    catch
    {
    }
    return null;
}

#endregion     



No comments:

Post a Comment