tirsdag den 15. december 2009

Hello C# (CSharp)

I know there are a lot of good Hello Worlds to be written in script language but lets go to something a bit more interesting. Real programming - well the beginning of it.

Visual Studio is an expensive way to be a hobbyist programmer, so Microsoft has made a free version of the big studio - Express. First - download and install it from Microsofts website. (I will not write the link, its better to search for visual studio express)

Now when you have installed C# you can open the studio and create a new project.

You can get a lot of help from the templates, but for this blog I'll just create an empty project to give more understanding of what is happening.
Type in HelloWorld and press ok.

When the project is ready, go ahead and save it so we can see the files. So afterwards you can open explorer and goto the path of the project. Open the files in notepad to see what has happened. You'll find that most of the files are pretty readable and understandable.

Now go back to Visual Studio and see in the solution explorer at the right. Here you can see the project which still is quite empty.

Let's get started
Rightclick the references folder in visual studio and choose "Add Reference". In the dialog scroll down to System.Windows.Forms. Select it and press ok. Now you can see that you have a reference in the References folder.
Right click the project and choose "Add|New item".


Select Code File and lets call it Program.cs (cs is for CSharp) and click Add. Now you have a very empty file. Add these lines to the file.

namespace HelloWorld

{
    public static class HelloWorld
    {
        public static void Main()
        {
            System.Windows.Forms.MessageBox.Show("Hello world");
        }
    }
}
 
If you press F5 to run the program a command line box enters and a dialog which says: "Hello world". Wow.

søndag den 1. november 2009

Hello Internet Explorer Favorites

For quite a while I have searched for a way to share my internet explorer favorites over more computers without having to put them on a USB-key or something. Here is the solution:

At http://www.getdropbox.com/ you can download a fancy little feature to your computer, which gives you 2 Gb storage on the internet for free. But unlike some of the other online storage companies this one lets you have it all offline so you can use the data even when you accidentially is offline.

So if you download and install the Dropbox program and create an account, you will have a folder for those files. In this folder you make a Favorites folder. Edit the registry. Choose start + run (Windows key + r) and type regedit. Browse to "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" and edit the Favorites value to your new folder. Important: I don't know why, but for some reason it wouldn't work unless I used  %USERPROFILE%.

Now (after a restart of IE) your Internet Explorer points to another directory which is backed up via Dropbox. Do the same for all the computers you want to use your favorites from.

Voila.. The favorites are updated automatically .

onsdag den 23. september 2009

Hello Virtual PC Part I

Today I'll start a larger hello, and its not scripting. It's a setup. The setup of a Virtual PC testing environment.

 
Lets start with the basics - downloading and installing virtual PC. You can find it here: http://www.microsoft.com/windows/virtual-pc/ - and if not.. then google it. Run the installation and follow the instructions.
After the installation I recommend that you  alter the keyboard settings. Expecially if you are danish. To do that just follow File|Options which leads to this dialog.

 
This is the first part of making a test environment.. But don't worry it gets more exciting.

 
Loopback network
We'll need a loopback network. I have grabbed this from Microsoft support.
To manually install the Microsoft Loopback adapter in Windows XP, follow these steps:
  • Click Start, and then click Control Panel.
  • If you are in Classic view, click Switch to Category View under Control Panel in the left pane.
  • Double-click Printers and Other Hardware, and then click Next.
  • Under See Also in the left pane, click Add Hardware,and then click Next.
  • Click Yes, I have already connected the hardware, and then click Next.
  • At the bottom of the list, click Add a new hardware device, and then click Next.
  • Click Install the hardware that I manually select from a list, and then click Next.
  • Click Network adapters, and then click Next.
  • In the Manufacturer box, click Microsoft.
  • In the Network Adapter box, click Microsoft Loopback Adapter, and then click Next.
  • Click Finish.
http://support.microsoft.com/kb/839013

Directory structure
I think the best structure for the environment is as follows. I use c:\vpc\ as the virtual pc root, and under this I have
  • baseimages
  • configuration
  • install

torsdag den 17. september 2009

Hello file

Lets try something a little more interesting. Files.

1. Make a vbs-file and add these lines

Option Explicit


' Declare the variables
Dim oFileSystemObject
Dim oTestFile

Set oFileSystemObject = CreateObjec("Scripting.FileSystemObject")

' Write to the file
Set oTestFile = oFileSystemObject.CreateTextFile("C:\HelloWorld.txt")

oTestFile.WriteLine "Hello World!"

oTestFile.Close
set oTestFile = nothing

' Read from the file
set oTestFile = oFileSystemObject.OpenTextFile("C:\HelloWorld.txt")

While not oTestFile.AtEndOfStream
wscript.echo oTestFile.Readline
wend
oTestFile.Close
set oTestFile = nothing
set oFileSystemObject = nothing

2. Save and run the file.

You should see a dialog with Hello World! Furthermore you should also could see a newly created textfile which contains Hello World!

Now, lets see how this works:
First we instantiate a Scripting.FileSystemObject, which is a standard ActiveX component. It can be used to a lot of the basic filehandling commands. Copying, moving, deleting, creation etc.
We then use its CreateTextFile method to instantiate a textstream object (oTestFile). That object can be used to write lines to the file.
Then we close the file so we can open it again and finally we get rid of the object from the memory by setting it to nothing.

To get som output, we use the oFileSystemObject to open the file again into the oTestFile textstream. We use the ReadLine method until the file is at its end to write all the lines in the file. And again close it and get rid of the objects.

A little tip you can try is to write more lines in the file and see that it reads them all.

Until next time.. Say hello to the world from me.

søndag den 13. september 2009

JScript

The windows scripting host also supports javascript. This is also an easy way to script your computer.
Same procedure as before.
1. Make a textfile and rename it to HelloWorld.js
2. Add this line (notice, that JScript is case sensitive)
WScript.Echo("Hello World!");

3. Save the file and doubleclick

Voilá your third Hello World

fredag den 11. september 2009

VB-Script

Now to another easy way to script your computer - throug vbscript. VB stands for Visual Basic, and perhaps you have heard about it.

Again I will start with a very easy script.
1. Make a file - the same way as in the first blog, but this time it should be renamed to HelloWorld.VBS
2. Edit the file and add these lines

Option Explicit
Wscript.echo "Hello World!"
msgbox "Hello world!"

3. Save the file and doubleclick.

You should see two dialog boxes on your screen with "Hello World!" and you might wonder why I showed you both methods, but it has a very good reason.

Try the following:
1. Click StartRun and write "wscript \helloworld.vbs" where path is the path to the file.. (TIP: You can drag the file to the textfield) - the same thing happens.
2. Click StartRun and write "cscript \helloworld.vbs" - notice what happens.

The wscript.echo checks if its running in a commandline or a windows environment and writes the text in the right environment. Msgbox allways uses a dialog. If you dont have a chance for user interaction, you do not want a dialog box that stops the script.

I haven't mentioned the first line (Option Explicit) yet. Actually it's not important in this example, but I personally thinks it's a good thing to learn from the start. When you put Option Explicit in the start of a vb-script, the interpreter demands that all variables are declared. If you don't understand this right now - don't worry, i'll get back to it.

Hello world

Hello world is typically the first testprogramme you write to see how a tecnique works and this blog is dedicated to first testprogrammes in all kind of languages some of them are easy and others might be a bit more complecated.
If you have ideas to a good blogpost then write me.
But lets get startet.
The first "Hello world" is a batch script - very very simple.
1. Make a .bat-file. The easiest way is to rightclick in a directory and pick New -> Text Document . Rename the file to HelloWorld.Bat.








notice: if you dont see the .txt extension in the filename you have to setup windows to see known extensions.
2. rightclick the file and select edit
3. in the fileeditor (typically notepad from standard) write following:
@echo off
echo Hello World
pause
4. close the editor and doubleclick the file


And now.. What actually happens.

The first line @echo off actually tells the system not to write anything unless you tell it to. The @-sign is only there to keep it from writing that line.. Confusing? Well maybe :-)

The second line echo Hello World tells the system to write Hello World.

The last line - pause tells the system to wait for a userinput.


Your first Hello World .. here you go :-)