How to create a virtual directory in IIS 6.0

System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot();
string sError;
vr.Create("IIS://localhost/W3SVC/" + GetSiteID(name of parent website) + "/Root", pathToFiles, nameofdirectory, out sError);


where GetSiteID is defined like


private static string GetSiteID(string strWebsiteName)
{
string strSiteID = string.Empty;

System.DirectoryServices.DirectoryEntry directoryEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/");

foreach (System.DirectoryServices.DirectoryEntry tempDirectoryEntry in directoryEntry.Children)
{
if (Convert.ToString(tempDirectoryEntry.Properties["ServerComment"].Value).ToLower() == strDirectoryName.ToLower())
{
strSiteID = tempDirectoryEntry.Name;
break;
}
}

return strSiteID;

}


Check if a temp table exists (TSQL)

if object_id('##temp_table') is null print 'DOES NOT EXIST' else print 'EXISTS'

How to convert a string to a byte array (C#)

System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
byte[] byteArray = encoding.GetBytes("your string");


The opposite conversion (byte array to string) looks like this:

System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
string myString = enc.GetString(byteArray);

How to get the list of files in a specified folder (C#)

DirectoryInfo dInfo = new DirectoryInfo("folder path");
FileInfo[] fInfo = dInfo.GetFiles("*.xml");
foreach (FileInfo f in fInfo)
{
...
}

Working with folders (C#)

How to check if a folder exists:

if (!Directory.Exists("folder name"))
...


How to create a folder:

Directory.CreateDirectory("folder name");

How to open a web page from a windows form (C#)

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "iexplore";
proc.StartInfo.Arguments = "www.yahoo.com";
proc.Start();

How to make a TreeView node editable on double click (C#)

The first thing we need to do (usually via the designer) is to set the LabelEdit property of the TreeView to true. Then we go to the handler method of the NodeMouseDoubleClick event and write as follows:

private void treeListe_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node == null)
return; //this is just to be on the safe side
//normally e.Node is the node where we double clicked and cannot be null

e.Node.BeginEdit();

}

How to resize an image loaded from disk (C#)

Bitmap originalBmp = (Bitmap)Image.FromFile(filename);
Bitmap newBmp = new Bitmap(originalBmp, newSize);

where filename is the name of the file we want to load (including path - such as C://Images/Pic1) and newSize is the wanted size.

Generate row ID (T-SQL)

Let's assume we have all our data in a table MyTable, with a column named MyTableID, that we want to fill with unique consecutive values.

DECLARE @Index BIGINT
SET @Index = 0

UPDATE MyTable
SET @Index = MyTableID = @Index + 1

How to declare a function (T-SQL)

CREATE FUNCTION MyFunction
(
@EntryParam BIGINT
)
RETURNS BIGINT --the returned type
AS
BEGIN
DECLARE @ReturnVar AS BIGINT -- the var to keep the return value
..... -- set @ReturnVar's value
RETURN @ReturnVar
END