Thursday, July 17, 2008

Calling an exe from Windows Service

Some times we may need to call an exe from our windows service program. The following code can be used to call an exe from an appication. The sample code is for VB.NET applications. Here the calculator (calc.exe) is called.


Dim startInfo As System.Diagnostics.ProcessStartInfo
Dim pStart As New System.Diagnostics.Process

'Start the process

startInfo = New System.Diagnostics.ProcessStartInfo("calc.exe")

pStart.StartInfo = startInfo

pStart.Start()

pStart.WaitForExit()

pStart.Close()

Here
pStart.WaitForExit() makes your application wait till the called exe is closed.
pStart.Close() will free all the resources that are associated with this component.

This will work if you are using a windows Application. But in a windows service this will not work straight, you need to change some setting for the windows service. A service won't be able to show the GUI of an application, so if the application you are calling has any GUI you won't be able to see it. But the process will get kicked off and you can see the evidence in TaskManager. But we can enable the service to popup the UI by enabling this settings.
Allow Services to interact with desktop
  1. Start > Run > Services.msc
  2. Locate the services which you have created
  3. Right click and select properties.
  4. Select "Log On" tab on the top of the screen
  5. Select "Allow Services to interact with desktop" check box.
  6. Click on "Apply" and start the services.
If you want to kill an application during Windows Service Stop, try the following code
Dim myProcess As Process
Dim myProcesses As Process() = Process.GetProcessesByName("calc")
For Each myProcess In myProcesses
myProcess.Kill()
Next

Friday, June 6, 2008

How to send email using C#

This article explains the steps to send email from your c# windows applications.

Send Email using C#.NET (Visual Studio 2005)

A detailed explanation of SmtpClient Class from msdn .NET Framework Developer Center is provided in the following link.

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Tuesday, April 22, 2008

Show or Hide system tables/stored procedures in SQL Server Enterprise Manager

Some times you may need to see the system tables or system stored procedures/views or may want to hide it so that only user created objects will be visible.
Follow these Steps...
In the SQL Server Enterprise Manager,

  • Right-click the server node under "SQL Server Group" (Say Local)
  • Select "Edit SQL Server Registration properties..."
  • Check/UnCheck "Show system databases and system objects"
  • Click Ok, thats it Done.

This will work for SQL Server 7.0 and SQL Server 2000.