Monday, 28 September 2009

Asp.NET redirect

Response.Redirect("http://www.microsoft.com")

Friday, 25 September 2009

Umbraco C# Open Source CMS Installation

  • Install IIS on your windows machine if it’s not there yet 

Control Panel
Add or Remove Programs
Add/Remove Windows Components (on the left)

Make the following changes after the installation

    • SQL Server Configuration Manager
      • Go to SQL Native Client Configuration/Client Protocols and Enable TCP/IP & Named Pipes
    • SQL Server Surface Area Configuration
      • Go to Surface Configuration for Services and Connections/Database Engine/Remove Connections and set remote connections to TCP/IP and Named Pipes
    • SQL Server Management Studio
      • Right click on top node (database name) and go to Properties/Security and set Server Authentication to SQL Server and Windows Authentication mode (mixed mode)
      • Enable sa account (temporarily, only for Umbraco installation). Go to Security/Logins, right click on sa account choose Properties. On General Page set password (if not set yet) and on Status Page set Login to Enabled.

Allows to have multiple IIS websites on windows machine. Umbraco can only run on the top level websites (not in virtual directory)

  • Create new IIS website using XP PRO IIS Admin.

Set it up to run on a different port (like 8081). If you want to use default website you can skip this step.

Simplifies the whole installation process as it downloads all the dependencies like .Net framework, SQL Server etc..

  • Install Umbraco CMS using the Install Now button which is using Microsoft Web Platform

Web platform installer will download and install all necessary dependencies.

When installing Umbraco, pick Web Site name you created earlier and do not specify anything in Umbraco application name (it doesn’t work if you do)

Choose option to create database for you (rather than using existing one). Use sa account as Database Administrator. Make sure to choose secure password for your Umbraco database user as it’s failing if it’s not secure enough.

  • Start Umbraco

After successful installation click Launch Umbraco link in Web Platform Installation or go to http://localhost:8081/ (or other port you picked for your website). The Umbraco will launch Configuration Wizard for your installation.

Tuesday, 15 September 2009

Open command line from context menu

This is the x64 version of Open Command Windows Here from PowerToys for Windows XP

Create .reg file with this script and execute it.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Prompt]
@="Open Command Window Here"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Prompt\command]
@="Cmd.exe /k pushd %L"

Open cygwin command line from context menu

This script will add extra option to context menu to open cygwin command line. Create .reg file with this script and execute it.

Windows x86

REGEDIT4

[HKEY_CLASSES_ROOT\Directory\shell\BashHere]
@="&Open Cygwin Window Here"

[HKEY_CLASSES_ROOT\Directory\shell\BashHere\command]
@="c:\\cygwin\\bin\\bash.exe --login -c \"cd '%1' ; exec /bin/bash -rcfile ~/.bashrc\""

[HKEY_CLASSES_ROOT\Drive\shell\BashHere]
@="&Open Cygwin Window Here"

[HKEY_CLASSES_ROOT\Drive\shell\BashHere\command]
@="c:\\cygwin\\bin\\bash.exe --login -c \"cd '%1' ; exec /bin/bash -rcfile ~/.bashrc\""
Windows x64
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\BashHere]
@="&Open Cygwin Window Here"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\BashHere\command]
@="c:\\Development\\tools\\Cygwin\\bin\\bash.exe --login -c \"cd '%1' ; exec /bin/bash -rcfile ~/.bashrc\""

Monday, 14 September 2009

SQL Server Copy Table

This SQL will create another table from the select statement

SELECT *
INTO table2
FROM table1

Thursday, 10 September 2009

C# LoopFileSystemWatcher

This is another implementation of FileSystemWatcher as FileSystemWatcher doesn’t work in all cases.

public class LoopFileSystemWatcher
{
public delegate void LoopFileSystemEventHandler(object source, LoopFileSystemEvent e);
public event LoopFileSystemEventHandler created;

readonly List<string> fileCache = new List<string>();

private readonly string folderPath;
private readonly string fileFilter;
private readonly int checkFrequency;

private bool cancelled = false;

public LoopFileSystemWatcher(string folderPath, string fileFilter, int checkFrequency)
{
this.folderPath = folderPath;
this.fileFilter = fileFilter;
this.checkFrequency = checkFrequency;

DirectoryInfo dir = new DirectoryInfo(folderPath);
foreach (FileInfo file in dir.GetFiles(fileFilter))
{
fileCache.Add(file.Name);
}
}

public void Wait(int waitSeconds)
{
Thread t = new Thread(delegate() { WaitThread(waitSeconds,false); });
t.Start();
}

public FileInfo WaitOnce(int waitSeconds)
{
return WaitThread(waitSeconds,true);
}

private FileInfo WaitThread(int waitSeconds, bool waitOnce)
{
FileInfo lastFile=null;
DirectoryInfo dir = new DirectoryInfo(folderPath);

DateTime future = DateTime.Now.AddSeconds(waitSeconds);
while(DateTime.Now.CompareTo(future)<0)
{
if(cancelled)
{
return null;
}

foreach (FileInfo file in dir.GetFiles(fileFilter))
{
if(!fileCache.Contains(file.Name))
{
fireLoopFileSystemEvent(file);
fileCache.Add(file.Name);
lastFile = file;
if(waitOnce)
{
return file;
}
}
}
Thread.Sleep(checkFrequency * 1000);
}
return lastFile;
}

public void Cancel()
{
this.cancelled = true;
}

private void fireLoopFileSystemEvent(FileInfo file)
{
if (created != null)
{
created(this, new LoopFileSystemEvent(file));
}
}
}

public class LoopFileSystemEvent : EventArgs
{
private readonly FileInfo file;

public LoopFileSystemEvent(FileInfo file)
{
this.file = file;
}

public FileInfo File
{
get { return this.file; }
}
}

Tuesday, 8 September 2009

C# Pub/Sub example

public class PubSub
{
readonly Queue<string> queue = new Queue<string>();

object locker = new object();

public string Subscribe()
{
string text;
lock (locker)
{
while(queue.Count== 0)
{
Monitor.Wait(locker);
}
text = queue.Dequeue();
}
return text;
}

public void Publish(string request)
{
lock (locker)
{
queue.Enqueue(request);
Monitor.PulseAll(locker);
}
}
}

class Program
{
Random r = new Random();

public Program()
{
PubSub pubSub = new PubSub();

Thread t1 = new Thread(delegate() { Publish(pubSub); });
Thread t2 = new Thread(delegate() { Subscribe(pubSub); });

t1.Start();
t2.Start();

t1.Join();
t2.Join();

Console.WriteLine("Completed.");

Console.ReadLine();
}

void Publish(PubSub pubSub)
{
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(r.Next(10, 100));
pubSub.Publish(string.Format("Publishing {0}",i));
}
pubSub.Publish(null);
}

void Subscribe(PubSub pubSub)
{
while(true)
{
string text = pubSub.Subscribe();
if(text==null) return;
Console.WriteLine(text);
}
}

static void Main(string[] args)
{
new Program();
}
}