Let's Talk About .NET, Java, and Various File Formats!

Archive for the ‘Microsoft .NET’ Category

Read File from Disk into MemoryStream using C# or VB.NET

Reading a file from the disk into the MemoryStream is a very common requirement while working with files in your .NET applications. It is important to read the files to MemoryStream when you want to process the file inside memory. For example, when you want to render a file from the server to the browser. This is very simple and you can use the following code snippet to read the file contents from the disk to the MemoryStream.

C# Code Snippet:

//open file from the disk (file path is the path to the file to be opened)
using (FileStream fileStream = File.OpenRead(filePath))
//create new MemoryStream object
MemoryStream memStream = new MemoryStream();
//read file to MemoryStream
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

VB.NET Code Snippet:

‘open file from the disk (file path is the path to the file to be opened)
Using fileStream As FileStream = File.OpenRead(filePath)
‘create new MemoryStream object
Dim memStream As New MemoryStream()
‘read file to MemoryStream
fileStream.Read(memStream.GetBuffer(), 0, CInt(Fix(fileStream.Length)))
End Using

Rendering PDF Files to Browser using .NET Code

In the .NET applications, we some times need to render the PDF files to the browser using our code — C# or VB.NET etc. It’s not a big deal! You only need to use Response object to send the file to the browser. The only thing you need to take care of is the use of proper methods and attributes.

First of all, we need to save the PDF document to a MemoryStream object. For example, we have a MemoryStream object named outStream and we need to render it to the browser. The following code snippet can be used to render the file:

//create new MemoryStream object and add PDF file’s content to outStream.
MemoryStream outStream = new MemoryStream();

//specify the duration of time before a page cached on a browser expires
Response.Expires = 0;

//specify the property to buffer the output page
Response.Buffer = true;

//erase any buffered HTML output

//add a new HTML header and value to the Response sent to the client
Response.AddHeader(“content-disposition”, “inline; filename=” + “output.pdf”);

//specify the HTTP content type for Response as Pdf
Response.ContentType = “application/pdf”;

//write specified information of current HTTP output to Byte array

//close the output stream

//end the processing of the current page to ensure that no other HTML content is sent

You need to use AddHeader method of Response object to add header and value to the response sent to the client. Content-Disposition response header field is used to convey additional information about how to process the response, and also to attach additional metadata, such as filename. The PDF will be viewed in the PDF viewer plugin installed for the browser. You can see a practical example of rendering the PDF file to browser in Aspose.Pdf Demo. In order to view the source code, please click on the Source tab.

Create Zip File using C# with the Help of Free Zip Library

ZIP file format allows you to make an archive of a set of files and compress those files in the package. The Zip file may contain the files without any compression, just for the sake of archive. The Zip format supports many algorithms. Zip files use .zip extension and ‘application/zip’ MIME type.

There are many software and tools for creating Zip files manually. However, software developers sometimes need to create the Zip files using their code. In this post, I’ll show you how to create Zip package using .NET application. I’m going to use C# for creating the sample for this post.

Although, creating Zip files from scratch and working with related algorithms is very complex and time consuming task. However, there are some free libraries which you can use in your code. One such free library is DotNetZip library. You can download this free library from codeplex.com. This library helps you Zip and Unzip files and folders in your .NET code. You can use this library in a variety of .NET applications.

In order to create a Zip file, you need to find the Ionic.Zip DLL which you can find in Tools folder inside the DotNetZip package you downloaded from codeplex.com. Once you have added the reference to this DLL, use the Ionic.Zip namespace in your code as shown below:

//Ionic library to create Zip files
using Ionic.Zip;

After that you can use the following code to Zip the files and folders into a single package:

//create ZipFile class
ZipFile zipFile = new ZipFile();
//add whole directory in the Zip package
zipFile.AddDirectory(@”C:\Files to Zip\html”,”/package/html”);
zipFile.AddDirectory(@”C:\Files to Zip\data”, “/package/data”);
//add particular file in the package
zipFile.AddFile(@”C:\Files to Zip\main.txt”,”/package/”);
//save output Zip file

AddDirectory method allows you to add a whole directory in a Zip file. First parameter is the path to the source directory and the second parameter represents the path in the Zip archive. In this example, I have mapped html and data folders from the source directory to the folders in the package directory in Zip archive. Similarly, AddFile method allows you to add a single file in the Zip package. In this case, I have added main.txt file in the root folder inside the archive.Finally, you can save the output PDF with the help of Save method of ZipFile class.

In this post, you have got an idea that how simple it is to create a Zip package in your .NET applications with the help of free Zip library. In my next post, I’ll share some other good programming tip with you guys.

Verify .NET Assembly for Type-Safe Code

Some times, developers need to produce the CIL in such a way that it meets type safety requirements. This type-safe code can only be generated by avoiding certain language constructs. In order to make sure that the .NET assembly contains type-safe code, you need to verify the assembly. The assembly can be verified using PEVerify utility from Microsoft.

If you have installed Visual Studio on your computer, this utility (peverify.exe) will already be there. It provides various options, however the simplest way to verify the assembly is to use peverify.exe along with the assembly path/name.

It will take a while and show you the result. If the assembly contains type-safe code, the following message will be shown on the command window: All Classes and Methods in <assembly path/name> Verified.