Skip to main content

Posts

How to compress and decompress data in c#

In this post,I will show you how to compress and decompress data in c# using GzipStream class.Below is the code for the same
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.IO.Compression; namespace CompressDecompress { class Program { conststring CompressFilePath = @"C:\temp\data.zip"; conststring Filename = @"C:\temp\data.txt"; privateconststring DeCompressFilePath = @"C:\temp\data1.txt"; staticvoid Main(string[] args) { Compress(Filename); Decompress(CompressFilePath); } privatestaticvoid Decompress(string compressFilePath) { using (FileStream inputStream = new FileStream(compressFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (FileStream outputStream = new FileStream(DeCompressFilePath, FileMode.Create, FileAccess.ReadWrite)) { using (GZipStream g…

How to read pdf document properties in asp.net

In this post, I will show you how to read the pdf document properties in asp.net.As we know that there is no- such  in-built class in .net framework that read the pdf document.So,for this demo,I will used third-party library iTextSharp.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PdfPropertiesReader.aspx.cs" Inherits="PdfPropertiesReader" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><divid="pdfProperties"runat="server"></div></div><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Read Pdf Properties"/></form>&…

How to convert IEnumerable to Html table in asp.net

In this post I will show how to convert IEnumerable to string (Html table).Assume you have a customer class which has three properties named FirstName,LastName and Age.Now,you want to display customer collection on UI.
For this I have created following method which retrives propertyname and value dynamically (using reflection) and return result as string
public static string ToHtmlTable(IEnumerable list, string tableSyle, string headerStyle, string rowStyle, string alternateRowStyle)<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><styletype="text/css"> .headerStyle { background-color: #abc;…

How to fetch header info from given url

In this post I will show how to fetch header info from given url using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; namespace SNIPPET_CS { publicclass HeaderInfo { publicstring Key { get; set; } publicstring Value { get; set; } publicstatic List<HeaderInfo> GetHttpResponseHeaders(string url) { List<HeaderInfo> headerList = new List<HeaderInfo>(); WebRequest webRequestObject = WebRequest.Create(url); WebResponse responseObject = webRequestObject.GetResponse(); if (responseObject != null) { headerList.AddRange(from string headerKey in responseObject.Headers select new HeaderInfo() { Key = headerKey, Value = responseObj…

How to send output of console application to file

In this example,I will show how to send console output to file in c# using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; namespace SNIPPET_CS { class Program { staticvoid Main(string[] args) { ConsoleOutput(); Console.WriteLine("Console output saved successfully"); Console.ReadLine(); } privatestaticvoid ConsoleOutput() { conststring applicationPath = @"D:\Windows\System32\ipconfig.exe"; //string ApplicationArguments = "-c -x";// Create a new process object Process processObj = new Process(); // StartInfo contains the startup information of// the new process processObj.StartInfo.FileName = applicationPath; //ProcessObj.StartInfo.Arguments = ApplicationArguments;// These two optional flags ensure that no DOS window// appear…

How to create an Enum value from the string

This is a simple code for converting string representation to Enum using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Blog_CS { publicenum State { New, Ready, Stop, Reset }; class Program { privatestaticvoid Main(string[] args) { conststring state = "New"; State currentState = (State)Enum.Parse(typeof(State), state); if (currentState == State.New) { Console.WriteLine("Run state"); } } } }

How to add custom sorting for a class with IComparable interface

Sometimes it necessary to sort your collection before showing it on UI.You can add custom sorting to you class just by inheriting it with  IComparable interface.IComparable interface contains one single method CompareTo.

For this post I have created a class Employee which has four properties FirstName,LastName,Age and Title.This class implements IComparable interface,which means instance of the class can be compared with other instance of this class.

using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Blog_CS { publicclass Employee : IComparable<Employee> { publicstring FirstName { get; set; } publicstring LastName { get; set; } publicint Age { get; set; } publicstring Title { get; set; } publicstatic List<Employee> Employees { get { returnnew List<Employee>() { new Employee(){FirstName = "F004",LastName = &…