Skip to main content

Posts

Showing posts from July, 2011

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 = &…

How to compute hash of a file in c#

This is a very simple code for calculating the hash of file in c# Namespace Required: using System.IO; using System.Security.Cryptography; Complete C# code using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Security.Cryptography; namespace Blog_CS { class Program { privateconststring FileName = @"D:\output.txt"; staticvoid Main(string[] args) { string fileHash = GetHashOfFile(FileName); Console.WriteLine(fileHash); } privatestaticstring GetHashOfFile(string fileName) { using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { //Create HashAlgorithm (MD5,SHA1...) HashAlgorithm hash = HashAlgorithm.Create("MD5"); byte[] hashByte = hash.ComputeHash(fileStream); return String.Join(",", has…

How to convert byte array to string in c#

This is a very simple code showing how to convert byte array to string in c# Note:Make sure that you have imported the namespace 'System.Text' using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Blog_CS { class Program { staticvoid Main(string[] args) { byte[] bytes = GetBytes("the quick brown fox jump over the lazy dog"); //Convert byte array to string.string message = Encoding.Default.GetString(bytes); Console.WriteLine(message); } privatestaticbyte[] GetBytes(string message) { //Make sure that you have imported 'System.Text' namespacereturn Encoding.Default.GetBytes(message); } } }

How to convert string to Byte array in c#

This is very simple code showing how to convert string to byte array in c# using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Blog_CS { class Program { staticvoid Main(string[] args) { conststring message = "the quick brown fox jump over the lazy dog"; byte[] bytes = Encoding.Default.GetBytes(message); Print(bytes); } privatestaticvoid Print(byte[] bytes) { Console.WriteLine(String.Join(",", bytes)); } } }

How to embed exe or resource file in c# assembly

Sometimes it is necessary to embed an executable (or any other file) into your application and later recreate it during runtime.In this post I will show you how to embed resource in assembly (application)

Open visual studio and create a new console application Right click on the project and add existing file (resource)



Each individual file must be added into your solution as an “Embedded Resource”. To do this right  click in the Solution Explorer then select:  Add-> Existing Item. Now locate the executable (or any other) file of your choice and then select it. You will now see this file in the Solution Explorer. Now right click on it and choose “Properties” Make sure "Build Action" has "Embedded Resource" selected.




C# Source Codeusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.IO; namespace Embed_Resource { class Program { /// <summary>/// This function read the na…

How to encrypt/decrypt data in asp.net using Symmetric algorithm

In this post I am going to show you how to encrypt and decrypt data in asp.net using Symmetric algorithm.Before going to implementation details let discuss some security related terms before.What is Hashing


Hashing is one-way algorithm, means once the data is hashed you can’t recovered it latter.
Hashing is a cryptographic function that is used to provide a secure fingerprint of data. A common usage you may have encountered is to check a file you have downloaded.
What is Encryption
If you want to secure your data, and latter you want to retrieve the original data. We used encryption because encryption is two-way processes. For encryption we requires following things
Key:Key is a piece of information that is used as an input parameter in encryption. The output of encryption is determined by the key.
Types of Encryption
are two main types of encryption and decryption algorithms, symmetric and asymmetric.Symmetric: In symmetric encryption we used same key for encryption and dec…

How to compress response in asp.net

In this post I will show you how to compress http response in asp.net for improving performance. Before going into details let's create a simple website with single page Default.aspx and add following code into it Default.aspx
<%@ 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></head><body><formid="form1"runat="server"><div><asp:GridViewID="grdPerson"runat="server"></asp:GridView></div></form></body></html> Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI…

How to Hash User Passwords in asp.net

In this post I will show you how to hash user password in asp.net Before going into implementation details,let first design the table structure.For the shake of simplicity I have taken only some fields. Right click on the website,add asp.net special folder App_Code. Right click on App_Code folder and add new item Linq to Sql classes and drag the table from Server Explorer to Linq to sql classes surface. Again right click App_Code and add new class named UserManager.cs inside App_Code folder.Now paste the following code in it using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Security.Cryptography; using System.Web.Security; publicclass UserManager { privatestaticstring CreateSalt(int size) { //Generate a cryptographic random number. RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] buff = newbyte[size]; rng.GetBytes(buff); // Return a Base64 string repre…