Sunday, June 14, 2009

Remove Focus Rectangle From Button

Article is about to remove focus rectangle or say cues from Button in window form application.

First let discuss what default focus rectangle. Please look at following image , in that image OK button has default focus so it display cues rectangle.

image 
(FlatStyle == System)

When we want to set background image of button to make button attractive in that case this default rectangle create problem with look and feel.

image
(FlatStyle = Standard)
 
 image
(FlatStyle = Flat)

In above two image need to set FlatApperance.BorderStyle =0. You can visualize that in all above case default focus rectangle make button look ugly with background image.

To remove this cues ( Default focus rectangle ) you need to set ShowFocusCues property of Button to false but this property does not directly available to Button. This property available in ButtonBase class with protected access specifier. In order to set this property to false we need to create class that inherits from Button or ButtonBase and set this property explicitly false.

class CustomButton : System.Windows.Forms.Button
    {
        protected override bool ShowFocusCues
        {
            get
            {
                return false;
            }
        }
}

image
Now see that default focus rectangle is removed.

More generic class for Button.

class CustomButton : System.Windows.Forms.Button
   {
       private bool _DisplayFocusCues = true;
       protected override bool ShowFocusCues
       {
           get
           {
               return _DisplayFocusCues;
           }
       }

       public bool DisplayFocusCues
       {
           get
           {
               return _DisplayFocusCues;
           }
           set
           {
               _DisplayFocusCues = value;
           }
       }
   }

Using this class you can set DisplayFocusCues at design time so CustomButton work with any case. ( Want to display focus rectangle or not).

image 

Hope this solution is help you to create button without cues.

Your suggestion is always invited.

Programmatically Install Window Service

This article is about to install / uninstall window service programmatically from C#. This is useful when you need to install / uninstall window service from some other application.

For this purpose you need to use ServiceProcessInstaller and ServiceInstaller class from another application. These two installer classes are responsible for Installation of service.

C# Example for Programmatically Install Window Service.

ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
ProcesServiceInstaller.Account =  ServiceAccount.User;
ProcesServiceInstaller.Username = "<<username>>";
ProcesServiceInstaller.Password = "<<password>>";

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new System.Configuration.Install.InstallContext();
String path = String.Format("/assemblypath={0}", @"<<path of executable of window service>>");
String[] cmdline = { path };

Context = new System.Configuration.Install.InstallContext("", cmdline);              
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.DisplayName = "MyService";
ServiceInstallerObj.Description = "MyService installer test";
ServiceInstallerObj.ServiceName = "MyService";
ServiceInstallerObj.StartType =  ServiceStartMode.Automatic;
ServiceInstallerObj.Parent = ProcesServiceInstaller;          

System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
ServiceInstallerObj.Install(state);

C# Example to Programmatically Uninstall Window Service.

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new InstallContext("<<log file path>>", null);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "MyService";
ServiceInstallerObj.Uninstall(null);

other article related to window service.

http://dotnetstep.blogspot.com/2009/06/passing-parameter-to-installutil.html

http://dotnetstep.blogspot.com/2009/06/install-window-service-without-using.html

http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html

http://dotnetstep.blogspot.com/2009/06/get-windowservice-executable-path-in.html

Please give your idea about this article.

Saturday, June 13, 2009

Passing Parameter to InstallUtil

When installing window service or any installer ( class inherits from install class) required custom parameter at run time , Need to pass as parameter of installutil.

Crating window service using article (http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html).  This article suggest you add installer ( ProjectInstaller). Now we use installer class events to configure ServiceProcessInstaller and ServiceInstaller as per parameter pass to installutil.

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
           this.BeforeInstall += new InstallEventHandler(ProjectInstaller_BeforeInstall);
            this.BeforeUninstall += new InstallEventHandler(ProjectInstaller_BeforeUninstall);
        }       

        void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e)
        {
            // Configure Account for Service Process.
            switch(this.Context.Parameters["Account"])
            {
                case "LocalService":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
                    break;
                case "LocalSystem":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                    break;
                case "NetworkService":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.NetworkService;
                    break;
                case "User":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
                    this.serviceProcessInstaller1.Username = this.Context.Parameters["UserName"];
                    this.serviceProcessInstaller1.Password = this.Context.Parameters["Password"];
                    break;
            }      
            // Configure ServiceName
            if(!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
            {               
                this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
            }
        }

        void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
            {
                this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
            }
        }
    }

Above ProjectInstaller class handle two events : BeforeInstall and BeforeUninstall. BeforeInstall is called just before install method of installer (ProjectInstaller) called via InstallUtil and same way BeforeUninstall called just before Uninstall method of projectinstaller class. After adding that two event handler use following to configure service using installutil.

1. Install
installutil /Account=User /UserName=admin /Password=admin /ServiceName=WinService1 /i WindowService1.exe

2. Uninstall
installutil /ServiceName=WinService1 /u WindowService1.exe

This will also help you configure WindowService1.exe with two different name and different account.
installutil /Account=User /UserName=admin /Password=admin        /ServiceName=WinService1 /i WindowService1.exe

installutil /Account=LocalSystem /ServiceName=WinService2 /i WindowService1.exe

Install window service without using installutil

Article is about to install window service without using InstallUtil utility.
Windows XP or Later has command line utility called sc . (This utility talks with service controller and with services from command line).
SC is a window base utility. AS explain in article http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html sc does not required ProjectInstaller.

1. Start service using sc
          sc start ServiceName

2. Stop serivce
         sc stop ServiceName

3. Delete window service
        sc delete servicename
        Note : During delete call if service is in running state then service is delete when nexttime service is stop or PC restart.

4. Create window service
       sc create ServiceName binpath= “c:\windowservice1\windowservice1.exe”

       Apart from above many option available . For that just go to command prompt and type sc create /?

  For example configure service running account.

sc create servicename binpath= “c:\windowservice1\windowservice1.exe” obj= administrator password= pass

Hope this article will help you .

Please give your comment or idea about this.

In above all case Service ServiceName ( Mark as blue) , don’t use display name.

 image

Install WindowService using installutil.

This article is about to install to window service using installutil.exe. InstallUtil.exe or simply say installutil utility comes up with .NET Framework 2.0.

Steps.
1. Create new Window Service Project.
image 
2. After that open services1.cs in design mode . It looks like this.
image 
3. Now right click on design window. It open popup menu with installer option.
image
4. When you click on Add Installer. It will will add new class called projectinstaller.cs. It contain serviceprocessinstaller1 and serviceinstaller component. ServiceProcess installer is used to configure service account and serviceinstaller contain information about service.
image
5. Apart from above ProjectInstaller.cs contain following code which is used to call from installutil.
image
In above code RunInstaller attribute marked as true. So InstallUtil take this thing into consideration and use this information to call installer.

To install using installutil open VS 2005/VS 2008 command prompt or go to  %windir%\Microsoft.NET\Framework\v2.0.50727.

installutil.exe /i  WindowService1.exe ( To Install ).

installutil.exe /u WindowSerivce1.exe ( To uninstall).

Installutil use Installer class (Projectinstaller) to install service.

If you mark RunInstlaller as false then this installutil not work.

If you don’t want to use installutil to install wndow service then go to http://dotnetstep.blogspot.com/2009/06/install-window-service-without-using.html.

Tuesday, June 9, 2009

Get WindowService Executable Path In .NET

WindowService has a property called “Path to Executable” . This property contains path of window service that is going to execute. When you need this path in .NET Application there is no direct way to do it . You can not get it either via ServiceControler or any other way.

image 
To get that you need to read Registry. All information about all window service installed on machine storead at following location.
HKLM\SYSTEM\CurrentControlSet\Services.

C# Code to read Service Registry.

RegistryKey services = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services");
if (services != null)
{
object pathtoexecutable = services.OpenSubKey(<<ServiceName>>).GetValue("ImagePath");
}

In Above code replace <<ServiceName>> with your service name.

Also this is a registry operation so you need proper permission to read registry and apply any other registry operation at your own risk.

Give your comment or any new idea about this task.

Sunday, June 7, 2009

Redirect console application output to another application in .NET

In .NET sometime it is required to execute system command and capture its result or data in application that execute that command.
For example you want to execute ipconfig command from .NET application and display its result in window application or need to read output of .NET console application.

For this purpose you need to use Process class that belongs to System.Diagnostics namespace.

//Create process class object
System.Diagnostics.Process p = new System.Diagnostics.Process();

// Set StartInfo property.
// Set redirection true for Error and output stream.
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;

// When you set redirection you must set UseShellExecute to false .
p.StartInfo.UseShellExecute = false;

// Set window style
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

// Set Command paramter.
p.StartInfo.FileName = "ipconfig";
p.StartInfo.Arguments = " /all";

// Set CreateNoWindow to true as window is not required here.
p.StartInfo.CreateNoWindow = true;

// Following two events are new in .NET framework 2.0 which allow us to capture data asynchronously.

p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(p_ErrorDataReceived);

// Start the process.
p.Start();

// Must call following two methods in order to capture data asynchronously.
p.BeginErrorReadLine();
p.BeginOutputReadLine();

// Wait for Process to Exit ( This is not must done ).
p.WaitForExit();

// Event Handler for ErrorStream.
void p_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
            System.Diagnostics.Debug.WriteLine(e.Data);   
}

// Event Handler for OutputStream.
void p_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
           System.Diagnostics.Debug.WriteLine(e.Data);
}

Even you can use this functionality to make utility like process command background and produce result in window application.

p.StartInfo.FileName = @"%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe";
p.StartInfo.Arguments = @" -v / -f -p C:\Blog\WebSite1 c:\WebSite1Compile";
// Note : Replace %windir% with your window directory. or use p.StartInfo.EnvironmentVariables["windir"]

Above is the one example to compile website application from window application.

Apart from this there is event called Exited associated with Process class object. In order to receive this event you need to set EnableRasingEvents property to true value.

Give your comment and idea on this article.

Saturday, June 6, 2009

DateTime operation in .NET Application

DateTime is a very important datatype and data in terms of realtime application. Some of the application that run timely manner and some of the application that generate data (log file) with time.
To generate such report you need to required store information about date and time.

In .NET there is datatype called DateTime. This contain all information about datetime but sometime problem with this too.

For example:

DateTime dt = DateTime.Now;  // 6/6/2009 09:35:00 PM.
This is used to get current current time of system. If you store this time in file or in a database and later you retrieve then you get that time. So at last “6/6/2009 09:35:00 PM” this is going to  store in a file.

Problem occur when you retrieve this later to display information and by some reason you make changes to current time zone of system or sometime timezone of application. So then also you get “6/6/2009 09:35:00 PM.” as data , which is not right.

So best way to store date is UTC format.

DateTime dt = DateTime.UtcNow and after retrieval convert it to LocalTime but for that also you have to confirm that stored DateTime is in Utc format.

DateTime structure contains  methods called ToBinary() and FromBinary().

long binaryDate  = DateTime.Now.ToBinary() ;

Now store ‘binaryDate’ value in database or text file. So later when you get this value.

DateTime dt =  DateTime.FromBinary(binaryDate).  This will generate correct Date and Time even after timezone changed. This is because of binaryDate( Long ) value contain information about TimeZone too. So it automatically adjust as per system timezone.

Apart from this DateTime structure contain one field called ‘Kind’. which contain current DateTime format kind and that is Utc , Local or Unspecified. Many of case when you store simple way DateTime and later you retrieve kind is unspecified. But when you store binary date and later you retrieve you get either Utc or Local.

Please give your comment or any new idea about storing datetime.

Usage of aspnet_compiler.exe and aspnet_merge.exe

Whenever asp.net website or webapplication publish,aspnet_compiler.exe is used for compilation. 

Example :

WebSite : c:\myProject\TestSite

1 . Go to Visual Studio Tool from program menu and start Visual studio command prompt.
                                     or
     If you do not have visual studio installed on your machine and just .NET Framework 2.0 installed on your machine then also you can use aspnet_compiler tool. 
- Start > run  and type cmd.
- set path=%windir%\Microsoft.NET\Framework\v2.0.50727
- aspnet_compiler –v / –p c:\myproject\testsite c:\testcompile
  
   It will compile c:\myproject\testsite and put published site in c:\testcompile. 
- When you compile site using above command site is not updatable after publish. 
-  If you want to site updatable after publish add –u option to it.
- It is good to add –f option in order to override existing published site.
- Fore more help just type aspnet_compiler /?

Now let discuss about aspnet_merge.exe tool. This tool is introduce later and it is used to merge all assemblies that is located in bin directory of published site. When you published website project many assemblies are in bin directories depending upon your option.

- In aspnet_compiler if you add –fixednames option then it will generate single assembles for each page otherwise it is generated per directory (this is default for asp.net website project).

- Main disadvantage of different assembles either way is problem with reflection. You are not able to get control and page information located in another assemblies without assemblies loading programmatically.

- aspnet_merge.exe tool merge all aspnet generated assemblies to single assemblies.

Example :

1. set path=%programfiles%\Microsoft SDKs\Windows\v6.0A\bin
2. aspnet_merge c:\testcompile
   This will create single DLL ( assembly ) in bin directory.
3. To set generated assembly name use this.
    aspnet_merge –o Single.dll c:\testcompile.

Hope this will help you.

Please give your comment and advice on this.