Posts Tagged c#
Formatting Console Output in C#
Posted by clifgriffin in Technology on May 5th, 2009
Yesterday I decided to modify a utlity I wrote to assist with copying Banner permissions from one user to another. I decided to add a list of the actual classes and objects that the destination user would receive as a result of the copy. This was a fairly easy modification, but as usual, I found myself spending most of my time on the easiest part of the project.
I naively assumed that C# would have some automagic way of display columns. While there may be a more official way to do it, nothing seemed to fit my scenario perfectly.
The average user has between 15 and 30 classes, but some have much more. I wanted to shorten the length, page wise, of the output. This was for purpose of readability. As I’m looping through a query result, my initial code looked something like this:
while (reader.Read()) { Console.WriteLine(reader[0].ToString()); }
I spent a good amount of time trying to use String.Format and things like it to try to turn this one column of output into two columns. The result? No success. Finally, I realized I was spending all my time trying to come up with the proper way to do something when there was most likely an easy, functional way to do this. I finally arrived at the following code:
int n = 0; while (reader.Read()) { if (n % 2 == 0) { Console.Write(reader[0].ToString().PadRight(30)); } else { Console.WriteLine(reader[0].ToString().PadRight(30)); } n++; }
The counter allows us to do an “every other time” operation by relying on whether or not the number is even or odd. Console.Write, as you should know, doesn’t append a return carriage, Console.WriteLine does.
PadRight(30) simply ensures that the output for each is a constant length. This prevents the “columns” from being misaligned.
There may be other, better ways to accomplish this. (And I welcome you to share them!) If you are dealing with a large amount of data and numerous columns, this is probably NOT the best approach. But if you’re looking for a functional, easy to implement means to an end, this may assist you.
The Virus Story: Operation Dust Bunny
Posted by clifgriffin in Evil, Personal, Technology on December 2nd, 2008
Note: I’m perfectly aware that what I wrote was not actually a virus. Virus definitions define it various ways ranging from a trojan to “potentially unwanted software”. In the purest sense it wasn’t a virus, trojan, worm, or any other derivative–which is fine by me. It wasn’t my intention to write a virus in the first place.
Every so often and a few of my close friends love to casually mention that I “wrote a virus”. This is usually in front of a cute girl or a large group of people who I don’t know very well.
This invariably leaves me in the position of either a) letting the remarks stand umodified, or b) qualifying the story with the facts. I usually choose b. A story like this might not be the key to a ladies heart (or anyone else’s for that matter), but I guess I get points for being unique.
It’s been a long time since these things happened so a lot of the details are hazy. I don’t think about it often and when I do think about it, it seems like it happened in another lifetime. Because of all of this, I’m going to write all of the details down to assist my memory, and possibly entertain you with the antics of a 19 year old with too much time and too little social life. Read the rest of this entry »
Converting HTML to a C# String
Posted by clifgriffin in Code on November 24th, 2008
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.
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
Installing a .NET Windows Service
Posted by clifgriffin in Technology on November 20th, 2008
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:
- Running
installutilfrom a cmd window doesn’t work. - 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:
- Go to the designer view of your main class. (Probably
service1.csif you didn’t rename it.)
- Right click in the gray area of the designer and click
Add Installer.
- This will create a file called ProjectInstaller.cs and take you to the designer view of this file where you should see:

- Click on
serviceProcessInstaller1and in the properties pane, change Account to LocalSystem. - Click on
serviceInstaller1and changeDescription,DisplayNameandServiceNameto something descriptive. - Change
StartTypeto 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.
Recent Comments