Tuesday, January 10, 2012

[Csharp] Create Log file

Log file is always useful for developer to fix bug application after deploy. Here is a simply class Logger, but it's very useful.


   1:  using System;
   2:  using System.Diagnostics;
   3:  using System.IO;
   4:   
   5:  namespace HHPS.CreditEducation.Frameworks.Commons
   6:  {
   7:      public class Logger
   8:      {
   9:          public const string LOG_FILE_NAME = "log.txt";
  10:   
  11:          public static void Write(string message = null, Exception exception = null)
  12:          {
  13:              string content = Environment.NewLine + DateTime.Now + " " +
  14:                               new StackTrace().GetFrame(1).GetMethod() + Environment.NewLine;
  15:   
  16:              if (!string.IsNullOrEmpty(message))
  17:              {
  18:                  content += message + Environment.NewLine;
  19:              }
  20:   
  21:              if (exception != null)
  22:              {
  23:                  content += exception.Message + Environment.NewLine;
  24:                  content += exception.StackTrace + Environment.NewLine;
  25:              }
  26:   
  27:              File.AppendAllText(Path.Combine(Environment.CurrentDirectory, LOG_FILE_NAME), content);
  28:          }
  29:      }
  30:  }

Log file will write current date time and name of parent method call it by this code:


   1:              string content = Environment.NewLine + DateTime.Now + " " +
   2:                               new StackTrace().GetFrame(1).GetMethod() + Environment.NewLine;

With a exception, log file write exception message and exception stack trace, very helpful to find bug.
When we use it in other class, we call like this:


   1:              try
   2:              {
   3:              }
   4:              catch (Exception ex)
   5:              {
   6:                  Logger.Write(null, ex);
   7:              }

Enjoy it!

Monday, January 9, 2012

Export DataGridView to MS Excel

Sometime, you want to export data from you application to MS excel file. Here is code demo.
Code simpy export excel from datagridview, with some little custom you can export something else.


   1:  using System;
   2:  using System.Windows.Forms;
   3:  using Microsoft.Office.Interop.Excel;
   4:  using Application = Microsoft.Office.Interop.Excel.Application;
   5:   
   6:  namespace CompareDatabaseTool
   7:  {
   8:      public static class Utils
   9:      {
  10:          public static void ExportToExcel(DataGridView dgvList, string fileName)
  11:          {
  12:              _Application app = new Application();
  13:              _Workbook workbook = app.Workbooks.Add(Type.Missing);
  14:   
  15:              //app.Visible = true;
  16:              _Worksheet worksheet = workbook.ActiveSheet;
  17:              worksheet.Name = "Exported";
  18:   
  19:              for (int i = 1; i <= dgvList.Columns.Count; i++)
  20:              {
  21:                  worksheet.Cells[1, i] = dgvList.Columns[i - 1].HeaderText;
  22:              }
  23:   
  24:              for (int i = 0; i < dgvList.Rows.Count; i++)
  25:              {
  26:                  for (int j = 0; j < dgvList.Columns.Count; j++)
  27:                  {
  28:                      worksheet.Cells[i + 2, j + 1] = dgvList[j, i].Value.ToString();
  29:                  }
  30:              }
  31:   
  32:              workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  33:                              Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing,
  34:                              Type.Missing, Type.Missing, Type.Missing);
  35:   
  36:              app.Quit();
  37:          }
  38:      }
  39:  }
Hope it useful!

Saturday, January 7, 2012

Begin with Android

I love C family, I don't study VB or Java much. But today, I have a new choice
This is a challenge, I am very enjoyable.
I dream about a new mobile phone use Android 4.0 in this year. And I can write some application for it. OMG! some little games, some utilities what I want to use. Some ideas in my mind:
 - Store special date such as: birthday, dead day... with 2 type calendar.
 - A family annals application, helps me remember it. It can sync with my computer.

So, how can start to study. We need download some think and some guideline:

 - Download Java SDK:

 - Download JRE

 - Download Eclipse (Classic version recommended)

SDK and Eclipse don't need to install. You can unzip and config follow:
http://developer.android.com/sdk/eclipse-adt.html#installing
http://developer.android.com/guide/developing/devices/managing-avds.html

You need to know, and develop use Java Core language and android virtual machine call Dalvik to compile.
Your application will run on a simulator call AVD (Android Virtual Device) by use AVD Manager.

Show here is hello android application:
youtube.com will show you what is detail. I think the quick way to study is try make application follow videos.

Enjoy it!