Get IWin32Window for the main window of Visual Studio

This is a way to wrap the main window of Visual Studio in a klass that implements the IWin32Window interface.

Public Class MainWinWrapper
    Implements System.Windows.Forms.IWin32Window
    Overridable ReadOnly Property Handle() As System.IntPtr _
            Implements System.Windows.Forms.IWin32Window.Handle
        Get
            Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd)
            Return iptr
        End Get
    End Property
End Class

This is useful for example when Visual Studio should be used as the owner of a MessageBox.

Dim res As DialogResult
res = MessageBox.Show(New MainWinWrapper(), "Hello!")

The code was found here.

Start process and redirect output

Function GetProcessText(ByVal process As String, ByVal param As String, _
        ByVal workingDir As String) As String
    Dim p As System.Diagnostics.Process = New System.Diagnostics.Process

    ' this is the name of the process we want to execute 
    p.StartInfo.FileName = process
    If Not (workingDir = "") Then
        p.StartInfo.WorkingDirectory = workingDir
    End If
    p.StartInfo.Arguments = param

    ' need to set this to false to redirect output
    p.StartInfo.UseShellExecute = False
    p.StartInfo.RedirectStandardOutput = True

    ' start the process 
    p.Start()

    ' read all the output
    ' here we could just read line by line and display it
    ' in an output window 
    Dim output As String = p.StandardOutput.ReadToEnd
    ' wait for the process to terminate 
    p.WaitForExit()
    Return output
End Function

Code from http://www.developerfusion.co.uk/show/4662/.

Initializing the contents of a password textfield in ASP.NET 2.0

Question
How do I initialize the contents of a password textfield in ASP.NET 2.0?

Simple anwser
You can’t! This doesn’t work (apparently this is for security reasons, although the exact motivation escapes me):

txtPassword.Text = "this sucks";

Better answer
Create an event handler for the TextBox’s PreRender event:

protected void txtPassword_PreRender(object sender, EventArgs e)
{
    TextBox txt = (TextBox)sender;
    txt.Attributes["value"] = txt.Text;
}

Voilá! Now you can assign to the Text property as in the first example.

(Credits for this tip to David Silverlight.)

Set credentials on a WebService connection

Assuming that si is a SoapHttpClientProtocol instance, this is how set the credentials:

si.Credentials = CredentialCache.DefaultCredentials;

(If the client is a web application then impersonation must be enabled.)

or

NetworkCredential myCred = 
    new NetworkCredential(username, password, domainName); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri(si.Url), "NTLM", myCred); 
si.Credentials = myCache; 
si.UnsafeAuthenticatedConnectionSharing = true;
si.ConnectionGroupName = username;

Server Error: Failed to access IIS metabase.

I have received this error several times when trying to run an ASP.NET 2.0 application on a fresh machine:

Server Error in '/Butler/EducationSI' Application.
--------------------------------------------------------------------

Failed to access IIS metabase.


Failed to access IIS metabase.jpg

The error normally goes away when reinstalling the .Net Framework 2.0 (it might be sufficient to repair the installation in “Add or Remove Programs” in the Control Panel).

Auto-expanding custom (and standard) types in the watch window

The default for complex types is to just display the type name in the debugger’s watch window. Some system types however display the values of one or more properties instead of the type name. What is displayed is configurable which also makes it possible to show more information about user-defined types. This is how it’s done:

  1. Edit C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\Debugger\mcee_cs.dat for C#, mcee_mc.dat for managed C++, vjsee.dat for J#. Sorry, no VB!
  2. Add/modify rows in the file. Each row describes how a type should be displayed. Example:
    <System.DateTime>=<Year>-<Month>-<Day> <Hour>:<Minute>:<Second>
  3. The first part is the type name, followed by ‘=’. The rest of the line contains property names surrounded with ‘< ' and '>‘, mixed with fixed text that is displayed as it is.

The example above will display DateTime values as "{2005-4-23 16:2:51}".

In addition to property names, one can also use expressions and method names.

Array-sorting using generics and anonymous methods

Alternative 1 (type safe sorting using generics):

private int ComparePass(Pass x, Pass y)
{
    if (x.Starttid &lt; y.Starttid) return -1;
    if (x.Starttid &gt; y.Starttid) return 1;
    return 0;
}
:
:
    Pass[] results = ...;
    Array.Sort&lt;Pass&gt;(results, ComparePass);
:

Alternative 2 (type safe sorting using generics and an anonymous method):

Pass[] results = ...;
Array.Sort&lt;Pass&gt;(results,
    delegate(Pass x, Pass y)
    {
        if (x.Starttid &lt; y.Starttid) return -1;
        if (x.Starttid &gt; y.Starttid) return 1;
        return 0;
    });

Alternative 3 (type safe sorting using generics and an anonymous method using a default comparer):

Pass[] results = ...;
Array.Sort&lt;Pass&gt;(results, 
    delegate(Pass x, Pass y)
    {
        return Comparer.Default.Compare(x.Starttid, y.Starttid);
    });