Posts Tagged Visual Studio

Converting HTML to a C# String

Today I was working on a project where it became useful to convert an HTML file into two C# strings.  As I only intended to do this one time, I opted to not do all of the string replacements necessary and decided to look for someone else who had already done the heavy lifting. 

In short time I found a singularly useful tool to do exactly what I needed.   The tool is named HTML To C# String and is published as an open source project under the GPL (or MIT) license. (it isn’t extremely clear which)

It's simple.

It's simple.

The only limitation I found was that this project does not preserve whitespace or line breaks. That’s not a problem if you’re dealing with simple html, but if you have anything fancy–like javascript, you might run into troubles. For example this:

var something = "This is such a cool var!"; //A comment about this var
var somethingElse = "blah";

becomes this:

var something = "This is such a cool var!"; //A comment about this var var somethingElse = "blah";

This will obviously not work.  I converted his project (which is only a few lines of code) into a Visual Studio 2008 project and tweaked it slightly.  You can download the result below.  I did not compile it with a setup as this utility is too simple to warrant an installation in my humble opinion.  Just run the executable in bin/Release. 

Download it now…
HTML To C# String-Mod

,

1 Comment

Installing a .NET Windows Service

You’ve written your service and now you want to install it but you’re having problems. Well have no fear, because I wasted valuable hours of my life re-figuring this out last night and will tell you everything you need to know.

Most instructions on the web simply tell you to run installutil against your executable. This sounds easy enough but has a couple of problems:

  1. Running installutil from a cmd window doesn’t work. 
  2. Once you run it, your windows service may or may not be properly configured for installutil.

To solve the first problem, run the Visual Studio 2008 Command Prompt which is located under Visual Studio 2008 in Start->All Programs.  To solve the second, you have to add an installer class to your service. (This is different from creating a deployment package.)  To add an installer class, follow these steps:

  1. Go to the designer view of your main class. (Probably service1.cs if you didn’t rename it.)
    Designer View 
  2. Right click in the gray area of the designer and click Add Installer.
    Add Installer 
  3. This will create a file called ProjectInstaller.cs and take you to the designer view of this file where you should see:
    Installer
  4. Click on serviceProcessInstaller1 and in the properties pane, change Account to LocalSystem.
  5. Click on serviceInstaller1 and change Description, DisplayName and ServiceName to something descriptive. 
  6. Change StartType to whatever you service needs. (Manual, Disabled, or Automatic)

Now, simply install the service. From the Visual Studio 2008 Command Prompt run:

installutil "Whatever Your Executable Is Named.exe"

If it is successful you should see a message like:

The transacted install has completed.

That’s all there is to it.

, , ,

No Comments

Using Timers in a C# Windows Service

For the most part, writing a windows service in Visual Studio.net is only marginally different than writing a windows application.  You may have difficulty attempting to implement some components you’ve learned to rely on. One such component is the timer.

In a windows application, you simply drag a Timer from the Toolbox and implement as normal.  Curiously, you can do the same in a service–it just doesn’t work.  This is because the timer in the toolbox is located in System.Windows.Forms. So how do you do it? It’s really quite simple.

First, add the following line to the top of your Windows Service project:

using System.Timers;

Next, add this line to top of your class (the section starting with public partial class..)

Timer timer1= new Timer();

Obviously you may want to pick a more descriptive name…just be consistent. Next we need to create the method that will fire each time the timer reaches 0.  

private void timer1_Elapsed(object sender, EventArgs e)
{
      //Some awesome code!
}

Lastly, we need to bind this method to the timer’s elapsed event and finish implementing the timer.  Find your service’s OnStart() method and add something like:

timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Start();

The first line above tells the service to run timer1_Elapsed everytime the timer1.Elapsed event is raised. The second line sets the timer1′s interval to 5 seconds (1000 milliseconds = 1 second).  Lastly we enable the timer and start it.

You may want to add this to your OnStop() method for completeness:

timer1.Enabled = false;

That’s all there is to it.  This should be all you need to know to implement a timer in your service.

, , ,

7 Comments

Thread Safe Calls To Windows Forms in Visual Studio 2005

I have been working on an increasingly complex monitoring application at work and recently chose to implement threads. This led to many thread safety issues. At first glance, it appeared I would have to rewrite every method or every call to a Form component to avoid cross-thread calls.

However, thanks to this genius article, I have made my code thread safe with almost 0 effort.

If only I had found it earlier.

, ,

2 Comments