顯示具有 C# Development 標籤的文章。 顯示所有文章
顯示具有 C# Development 標籤的文章。 顯示所有文章

2008年6月2日 星期一

Change Window Form Application Background from Embeded Resource

This is a note about changing the background image of .Net Win Form Application.
Assumption:

  1. the image resources is embedded.
  2. you have no idea about the full name of the background image. (tricky ...)
The following name space is required:
  • using System.Reflection;
  • using System.IO;
  • using System.Drawing;
Here is the code to load the resource from assembly.
  1. Assembly a = Assembly.GetExecutingAssembly();
  2. string[] resNames = a.GetManifestResourceNames();
  3. foreach (string s in resNames)
  4. {
  5. if (s.EndsWith(".png"))
  6. {
  7. Stream imgStream = a.GetManifestResourceStream(s);
  8. this.BackgroundImage = Bitmap.FromStream(imgStream) as Bitmap;
  9. break;
  10. }
  11. }
With using this code the resource can be load dynamically.

Reference
  1. http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75

2008年6月1日 星期日

.Net Internationalization

The internationalization is a big issue for application that will be deployed for different language environment.
Visual Studio 2005 don't integrate the ResGen is very strange thing. We need to do some extra job to integrate the resource file into our program.
So, this article is a document to describe the steps to let a C# application can support multi-language.
  • Use notepad create a text file. I named it as i18n.txt with following content
i18nTest.Name = Multi-language testing
i18nTest.Caption = Multi-language caption
i18nTest.bntClickMe = Please push me

  • Create a new text file for Tradition Chinese call i18n.zh-tw.txt
i18nTest.Name = 多國語系測試
i18nTest.Caption = 多國語系標題
i18nTest.bntClickMe = 請按我吧
(Be aware save the file with unicode.)

  • Using ResGen.exe (should be in C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin) compile the text file into resources file.
resgen i18n.zh-tw.txt ==> there should be a file called i18n.zh-tw.resources created.
  • Add the files generated by ResGen to project solution.
  • Add some code to get the resource string.
    • import
    • using System.Globalization;
      using System.Threading;
      using System.Resources;
    • declare & initialize a resource manager
public ResourceManager rm;
rm = System.Resources.ResourceManager.CreateFileBasedResourceManager("i18n",".",null);
"i18n" is your default language resource file name.

    • switch culture info
    • try
      {
      Thread.CurrentThread.CurrentCulture =
      new CultureInfo("zh-TW");
      }
      catch (Exception ex)
      {
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      }
      Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    • Get resource string
    • rm.GetString("i18nTest.Caption");
By using the code above, our program can change it language at runtime and by demand.

Reference
  1. http://msdn.microsoft.com/en-us/library/ccec7sz1.aspx
  2. http://www.codeproject.com/KB/cs/multilingual_pplication.aspx

2008年5月29日 星期四

Test Driven Development with NUnit

Test Driven Development (TDD) [1] is a methodology to develop software system. The methodology is an evolutionary approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring.
NUnit is a tool for .Net Platform to execute the TDD. You can download the install package from Nunit website [2].

The following section describe the basic steps for NUnit[2] integration.
  1. Import NUnit.framework.dll into project reference reference.
  2. Create test class with "using NUnit.Framework;"
  3. Add [TestFixture] attribute to identify the class is a test class
  4. Add [SetUp] attribute to a function which is used to setup the environment before test. (The test function will be called before each test function)
  5. Add [Test] attribute to a function which is used to identify the function is a unit test function.
  6. Run the test with NUnit Test Runner user interface. The runner programmer will ask you where is your test DLL, before you start to run the test.
In our project, we want to test our data access object(DAO). So, in the spring context will be initial in the setup function.

  1. #region declare dields
  2. private IApplicationContext ctx;
  3. #endregion

  1. [SetUp]
  2. public void SetUp()
  3. {
  4. BasicConfigurator.Configure();
  5. ctx =
  6. new XmlApplicationContext("assembly://PROJECT_NAME/NAME_SPACE/ApplicationContext.xml");
  7. }


Reference
  1. http://www.agiledata.org/essays/tdd.html
  2. http://www.nunit.org/index.php

2008年5月28日 星期三

C# Common.Logging Configuration

Logging mechanism is very important for service program. The log information is only data source to know what happen in the application especially in service like program.
Common.Logging is well integrated with Spring.NET framework, however, the configuration of the Common.Logging is really not pleasant experience. Two reasons make this configuration so difficult. The First is the version of the logging component is really confuse people. Second, forget the external configuration should be copied to output directory.
Be aware the logging components' version.

Common.Logging.dll 1.2.0.0
Common.Logging.Log4Net.dll 1.2.0.2
Common.Logging.NLog.dll 1.2.0.2
log4net.dll 1.2.10.0
NLog.dll 1.0.0.505

Here is the setting in App.config


<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4Net">
<arg key="configType" value="FILE-WATCH"/>
<arg key="configFile" value="~/log4net.config"/>
</factoryAdapter>
</logging>
</common>

log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>

</log4net>
</configuration>




C# windows service debuging skill

Recently, I rewrite an active MQ receiver program which is written in Java using Spring Framework. Basically the Java program works very well. However, I thought, reason to rewrite this program is to unify the deployment environment and provide an automatic startup mechanism. Hence, we choose C# Windows Service as our solution to solve the problem in Java.
It is not really painful for us to move the code from Java to C#, however to debug in C# service is really painful.
I found there are three solutions to solve the debug issue:
  1. using the process attach debug method by Visual Studio [1][2]
  2. using #if (DEBUG) #else declarative [3]
  3. Run Windows Service as a console[4]
I chose the third way to debug my program, because, using console mode is most intuited for me and the code is also straightforward.

Reference
  1. Praveen Moosad, Debugging Windows Services in C# and .NET ,http://www.c-sharpcorner.com/UploadFile/prvn_131971/DebuggingWindowsServices02062006014001AM/DebuggingWindowsServices.aspx?ArticleID=f5db56a4-8e1f-4de3-8fe2-1168603450ba, 2006.Arcane Code, Windows Services in C#: Debugging Windows Services (part 4), http://arcanecode.wordpress.com/2007/05/24/windows-services-in-c-debugging-windows-services-part-4/, 2007.
  2. Lee Humphries, Debugging Windows Services under Visual Studio .NET, http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx, 2006.
  3. Einar, Run Windows Service as a console program,http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program, 2007.