JG Vimalan's Blog

It is all about coding!

Installing font using C# setup project

One of my team member asked about installing font to the system when a C# application is installed. Here are the steps,

a. Create a C# setup project. Open ‘File System’. Right click on ”File System’ and select ‘Add Special Folder’, choose ‘Fonts Folder’.

b.  Right click ‘Fonts Folder’ and select Add->File

c. Browse and select the font file(s) you wish to install.

d. Ensure that, the register property of the font file is set to vsdrfFont.

e. Now, run the setup. Once the setup is completed, you can find that, any font dialog in the system displays the installed font.

If you wish the font should remain in the system even after uninstalling the application, set the permanent’ property of the font file to true.


February 22, 2011 Posted by | C#.NET | 12 Comments

Converting json string to C# object and vice versa

In order to convert a C# generic list to json string and vice versa,

Add reference to System.Web.Extensions in your project.

//Assume that, you have a generic list as shown below,
List<Patient> patientList = GetPatients();

Use Serialize method in JavaScriptSerializer class for serializing generic list to Json string.

System.Web.Script.Serialization.JavaScriptSerializer serializer =  new System.Web.Script.Serialization.JavaScriptSerializer();

//use Serialize method to convert patient list to json string

string JStr = serializer.Serialize(patientList);

//Now, the json string can be set to generic list of type patient as shown below,

List<Patient> nList = new List<Patient>();

nList = serializer.Deserialize<List<Patient> >(JStr);

DOWNLOAD the working sample here https://docs.google.com/file/d/0By3at1G2EGgOal9IMW5pQjNTNXM/edit (File->Download)

February 22, 2011 Posted by | Android, C#.NET, JSON | 7 Comments

Convert sByte [] to string

I got a task where I need to convert a compressed string in sbyte[] back to original string. The example string used here is “Hello World!!!Hello World!!!”

Here is the code,

sbyte[] sbyteData = new sbyte[] {
31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, 47, -54, 73, 81, 84, 84, -12, 64, -31, 1, 0, 111, 9, 127, -117, 28, 0, 0, 0 }; //compressed string
//Convert sbyte[] to byte[]
byte[] byteData = Array.ConvertAll(sbyteData, (a) => (byte)a);
System.IO.Stream stream = new System.IO.MemoryStream(byteData);

System.IO.Compression.GZipStream gstream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress, true);
byte[] resultBytes = new byte[byteData.Length];

//fill the resultBytes with decompressed byte array
int readResult = gstream.Read(resultBytes, 0, resultBytes.Length);

//you got the actual string here
string stringFromByteData = utf8Encoding.GetString(resultBytes);

February 22, 2011 Posted by | C#.NET | 1 Comment