Category: C#

September 4th, 2014 by ronny

I’ve made a short video to demonstrate how to create a installer for a windows service. Use HD resolution to see the details.

Happy installing!

Posted in C#, Programming

August 2nd, 2014 by ronny

invisible-iconSometimes it can be handy to hide your console application from the users, to avoid complications in the example. Here is how to do it.

First, we need to import a few dlls and a few functions from those dlls. We will also make a few variables for the show and hide parameter in ShowWindow.

In the class before the main function.

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private static extern IntPtr GetConsoleWindow();

const int SW_HIDE = 0;
const int SW_SHOW = 5;

Then we will run the function to get hold of the handle for the console window, and run the hide function (ShowWindow, with hide parameter).

// Hide the window
IntPtr hwnd;
hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);

To “unhide” the window, if you need to do that before the application ends.

// Show the window
IntPtr hwnd;
hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_SHOW);

That’s it really.

Happy hiding!

Posted in C#, Programming

May 3rd, 2014 by ronny

ms-visual-c-iconThere is plenty of areas to use this. For me, I use vb scripts on one of my software so users can make their own start up and shutdown scripts. And here is how to implement it into your application.

[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace cs_script1
class Program
static void Main(string[] args)
Console.WriteLine("Script info initializing…");
Process vbScript = new Process();
vbScript.StartInfo.FileName = @"cscript";
vbScript.StartInfo.WorkingDirectory = @"c:\scripts\";
vbScript.StartInfo.Arguments = "hello.vbs";
vbScript.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Console.WriteLine("Script started…..");
vbScript.WaitForExit(); // Remove if you don’t want the script to finish before continue
Console.WriteLine("Script executed….");

First we use the system.diagnostic to create a new process.
With the startInfo.FileName we type in the Windows Script Host (cscript).
Next line is just the working directory, where we work with our script files.
The script itself will be an argument for the script host.
WaitForExit, this one make sure the script is done before continuing with the c# application. If it is not needed for the application to wait for the script to exit, just remove that part.

Example hello.vbs

[sourcecode language=”vb”]
x=msgbox("Welcome to this script" ,0, "Welcome")

You can start as many instances of the script as you like. Just run the vbScript.Start() several times to start it more than once.

And that’s it really.

Happy scripting!

Posted in C#, Programming, Uncategorized, VB Script