(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: May 2002
Section: Q&A
Authors: Karl Peterson and Mattias Sjogren

C#	Print Environment Information
Listing 1	This code uses the properties and methods of the System.Environment class to print information about your execution environment to the console. You can reduce typing by using the using directive to create a shorter name alias for the classes you use often. 

using C = System.Console;
using E = System.Environment;

// ...

static void PrintEnvironmentInfo()
{
	C.WriteLine( "=== Command line arguments ===" );
	foreach ( string arg in E.GetCommandLineArgs() )
		C.WriteLine( arg );

	C.WriteLine();
	C.WriteLine( "=== Environment info ===" );
	C.WriteLine( "CommandLine = " + E.CommandLine );
	C.WriteLine( "CurrentDirectory = " + E.CurrentDirectory );
	C.WriteLine( "MachineName = " + E.MachineName );
	C.WriteLine( "OSVersion = " + E.OSVersion.ToString() );
	C.WriteLine( "SystemDirectory = " + E.SystemDirectory );
	C.WriteLine( "TickCount = " + E.TickCount );
	C.WriteLine( "UserDomainName = " + E.UserDomainName );
	C.WriteLine( "UserName = " + E.UserName );
	C.WriteLine( "(CLR) Version = " + E.Version.ToString() );
	C.WriteLine( "WorkingSet (bytes) = " +
		E.WorkingSet.ToString() );

	C.WriteLine();
	C.WriteLine( "=== Logical drives ===" );
	foreach ( string drive in E.GetLogicalDrives() )
		C.WriteLine( drive );
}

VB5, VB6	Enable or Disable CD AutoRun
Listing 2	You can prevent an inserted CD's AutoRun app from firing if your app is in the foreground and you respond with TRUE (1) to the shell's custom QueryCancelAutoPlay message. Note that the system has hardcoded the value of TRUE in this case, and you can't pass a VB-standard True as your response. In this demo, this quirk conveniently maps directly to the vbChecked and vbUnchecked constants.

Option Explicit

Private Declare Function RegisterWindowMessage _
	Lib "user32" Alias "RegisterWindowMessageA" _
	(ByVal lpString As String) As Long
Private Const RegMsg As String = _
	"QueryCancelAutoPlay"

Private m_RegMsg As Long

Private Sub Check1_Click()
	With Check1  'Style = 1 - Graphical
	If .Value = vbChecked Then
		.Caption = "Release to Allow AutoPlay"
	Else
		.Caption = "Depress to Suppress AutoPlay"
	End If
	End With
End Sub

Private Sub Form_Load()
	Check1.Value = vbChecked
	' Determine custom message value, and 
	' hook message stream of this form.
	m_RegMsg = RegisterWindowMessage(RegMsg)
	Call HookWindow(Me.hWnd, Me)
End Sub

Private Sub Form_Unload(Cancel As Integer)
	Call UnhookWindow(Me.hWnd)
End Sub

Friend Function WindowProc(hWnd As Long, _
	msg As Long, wp As Long, lp As Long) As Long
	Dim Result As Long
	Select Case msg
		Case m_RegMsg ' QueryCancelAutoPlay
			' TRUE: cancel AutoRun 
			'       *must* be 1, not -1!
			' FALSE: allow AutoRun
			Result = Check1.Value

		Case Else
			' Pass along to default window 
			' procedure.
			Result = InvokeWindowProc( _
				hWnd, msg, wp, lp)
	End Select

	' Return desired result code to Windows.
	WindowProc = Result
End Function

