Tag Archives: C#

How to Convert JSON to a Class in C#

I have been working on .Net projects where I need to work with JSON to a class in C#. I came across a web site created by Jonathan Keith using the popular JSON.Net library by James Newton-King.

The web site allows me to copy and paste JSON code to a form and by pressing the generate button I was able to get back a class that I can copy and use in my .Net project. This web site is very useful to me since I am also using the JSON.Net library in my current project to Deserialise JSON data in to an object.

http://json2csharp.com

json2csharp_web

C#: Joining an Array of Elements Into One Single String

If you need to convert an array to singe delimited string it is easy to do. Below is an example C# code to demonstrate how to join an array of integer values into a single delimited string:

using System;

namespace JoinArrayElements
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int[] numbers = new int[] { 1,2,3,4 };

			foreach (int num in numbers)
			{
				Console.WriteLine(num);
			}

			var strJoined = string.Join(",", Array.ConvertAll(numbers, Convert.ToString));

			Console.WriteLine ("Joined Integer Array as a Single String: {0}", strJoined);
		}
	}
}

I am using the string.Join() to join my array, the first parameter would be the delimiter character for this example I am using the comma “,”. Then the second parameter is the array that contains all the elements, but this parameter only takes an array of string. Okay we can fix this by converting the array of integers to an array of strings, by using the Array.ConvertAll() method and passing the array values to be converted and set the data type to convert to. Continue reading

C#: DataTable to CSV

 

Recently I wrote a small program to allow me to connect to a SQL database and export a selected table to a CSV file. So I needed a method that can take in a DataTable and the delimiter character to use. The returned result is a string which can be save straight to a text file.

Below is the method in my program, you may copy, modify and use it in your own program.

I use a StringBuilder class to build my lines of comma separated values and then return it as a String type. Currently the code only format string values with double quotes inserted around the value and the rest is a straight output.

Continue reading

Windows Phone 7: Write Your First App

Windows Phone

Got a Windows Phone 7 and want to write some cools apps for it? Microsoft has release some tools to the public so that programmers like yourself can get started! You actually don’t need a Windows Phone because the kit comes with an emulator. Anyways let get start by downloading the developer’s kit from the App Hub and press on “Download the free tools”.

The installation package includes the following:

  • Visual Studio 2010 Express
  • Windows Phone Emulator
  • Silver Light
  • XNA Game Studio 4.0
  • Microsoft Express Blend for Windows Phone
  • .NET Framework 4

Continue reading

C#: How to get your computer and user name.

If you want to get your computer and user name from Windows in C# try this by using the WindowsIdentity class from System.Security.Principal namespace:

<span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Text;
<span style="color: blue">using </span>System.Security.Principal;

<span style="color: blue">namespace </span>ConsoleApplication1
{
    <span style="color: blue">class </span><span style="color: #2b91af">Program
    </span>{
        <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args)
        {
            System.<span style="color: #2b91af">Console</span>.Out.WriteLine(<span style="color: #2b91af">WindowsIdentity</span>.GetCurrent().Name);
        }
    }
}

How to use a ProgressBar with a Cancel button.

image

It’s the Easter long weekend here in Perth Western Australia all the shops are closed today, so I am at home catching up with some house work and blogging.

Well in this blog I am going to demonstrate how to create a Progress Dialog Box to show the progress of your process and allowing the user to cancel out of the process.

First what you need to do is create a new Windows Form Application project or download the project from here.

Continue reading

How to split multi page TIFF image file with C#

Few days ago at work, I have been assigned a job to write a tool to split a .tif file that contains multiple pages. So below I have some code snippets that demonstrates how to use the .NET Framework and C# to achieve this, and we don’t need to use any third party libraries.

1) First you need to create a new Console Project called SplitTIFF.

2) Add the System.Drawing assembly to your Reference folder in your project:

3) In your code add the System.Drawing and System.Drawing.Imaging namespace.

3) Add the following code in your Main method:

Continue reading