How to check if a Form is Open in C# and VB.NET

To Check if a Form is open in C# and VB.NET you can use the snippet below.

Sample C#

public static bool FormIsOpen(FormCollection application, Type formType)
{
	//usage sample: FormIsOpen(Application.OpenForms,typeof(Form2)
	return Application.OpenForms.Cast<Form>().Any(openForm => openForm.GetType() == formType);
}

Sample VB.NET

Public Shared Function FormIsOpen(application__1 As FormCollection, formType As Type) As Boolean
	'usage sample: FormIsOpen(Application.OpenForms,GetType(Form2)
	Return Application.OpenForms.Cast(Of Form)().Any(Function(openForm) openForm.[GetType]() = formType)
End Function

How to open a file or folder using VBA

To open a file or folder using VBA you can sue the follwing snippet.
This snippet should also work in VB6.

Sample VBA

Dim path As String
Dim explorerPath As String

explorerPath = Environ$("WINDIR") + "\Explorer.exe "
path = "C:\Users\Codesnippets\Desktop\test"
Call Shell(explorerPath + Chr(34) + path + Chr(34), vbNormalFocus)

How to select a file using OpenFileDialog in C# and VB.NET

To select a file using OpenFileDialog in C# and VB.NET you can use the following snippet.

Sample C#

private string SelectFileDialog(string initialDirectory=@"C:\")
{
	using (var dialog = new OpenFileDialog())
	{
		dialog.Filter = @"All files (*.*)|*.*";
		dialog.InitialDirectory = initialDirectory;
		dialog.Title = @"Select a file";
		return (dialog.ShowDialog() == DialogResult.OK) ? dialog.FileName : string.Empty;
	}
}

Sample VB.NET

Private Function SelectFileDialog(Optional initialDirectory As String = "C:\") As String
	Using dialog = New OpenFileDialog()
		dialog.Filter = "All files (*.*)|*.*"
		dialog.InitialDirectory = initialDirectory
		dialog.Title = "Select a file"
		Return If((dialog.ShowDialog() = DialogResult.OK), dialog.FileName, String.Empty)
	End Using
End Function

OpenIfClosed DBConnection extension method for C# and VB.NET

This snippet will allow you to use the OpenIfClosed DBConnection extension method for C# and VB.NET.

Sample C#

public static void OpenIfClosed(this DbConnection connection)
{
	if (connection!=null && connection.ConnectionString!=String.Empty && connection.State == ConnectionState.Closed)
	{
		connection.Open();
	}
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Sub OpenIfClosed(connection As DbConnection)
	If connection IsNot Nothing AndAlso connection.ConnectionString <> String.Empty AndAlso connection.State = ConnectionState.Closed Then
		connection.Open()
	End If
End Sub

How to read a textfile line by line in VBA

To read a textfile line by line in VBA you can use the following snippet.
To read a textfile complete into a string you can use How to read a textfile in VBA.

Sample VBA

public sub ReadTextFileLinebyLine()
    Dim sFileName As String
    Dim iFile As Integer
    Dim sLine As String

	on error goto errorhandler
    sFileName = "C:\Users\Codesnippets\Desktop\Test.txt"
	
	If Len(Dir$(sFileName)) = 0 Then
        Exit Sub
    End If

    iFile = FreeFile()
    Open sFileName For Input As iFile

    Do While Not EOF(iFile)
        Line Input #iFile, sLine
        Debug.Print sLine 
    Loop

    Close iFile
	exit sub
	
errorhandler:
'handle the error your way
End Sub

How to open cd rom drives in VBScript

To open cd rom drives in VBScript you can use the following snippet.

Sample VBScript

Set wmPlayer = CreateObject("WMPlayer.OCX.7" )
Set colDrives = wmPlayer.cdromCollection

if colDrives.Count >= 1 then
        For i = 0 to colDrives.Count - 1
			colDrives.Item(i).Eject
        Next
End If

How to check if a file is locked in C# and VB.NET

To check if a file is locked in C# and VB.NET you can use the following snippet.

Sample C#

public bool IsFileLocked(FileInfo file)
{
	var stream = (FileStream) null;
	try
	{
		stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
	}
	catch (IOException ex)
	{
                //handle the exception your way
		return true;
	}
	finally
	{
		if (stream != null)
		{
			stream.Close();
		}
	}
	return false;
}

Sample VB.NET

Public Function IsFileLocked(file As FileInfo) As Boolean
	Dim stream = DirectCast(Nothing, FileStream)
	Try
		stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
	Catch generatedExceptionName As IOException
                'handle the exception your way
		Return True
	Finally
		If stream IsNot Nothing Then
			stream.Close()
		End If
	End Try
	Return False
End Function

How to check if a form is already open in C# and VB.NET

To check if a form is already open in C# and VB.NET you can use the following snippet.

Sample C#

public static bool IsFormOpen(Type FormType)
{
	foreach (var OpenForm in Application.OpenForms)
	{
		if (OpenForm.GetType() == FormType)
		{
			return true;
		}
	}
	return false;
}

Sample VB.NET

Public Shared Function IsFormOpen(FormType As Type) As Boolean
	For Each OpenForm As var In Application.OpenForms
		If OpenForm.GetType() = FormType Then
			Return True
		End If
	Next
	Return False
End Function

How to open a pdf at a specific page using AcroRd32Info in C# and VB.NET

To open a pdf at a specific page using AcroRd32Info in C# and VB.NET you can use the following snippet.

Sample C#

public static void OpenPdfAtPage(string document, int page) 
{
	object myProcess = new Process();
	object startInfo = new ProcessStartInfo();
	startInfo.FileName = "AcroRd32Info.exe";
	startInfo.Arguments = string.Format("/A \"page={0}\"{1}", page, document);
	myProcess.StartInfo = startInfo;
	myProcess.Start();
}

Sample VB.NET

Public Shared Sub OpenPdfAtPage(document As String, page As Integer)
	Dim myProcess = New Process()
	Dim startInfo = New ProcessStartInfo()
	startInfo.FileName = "AcroRd32Info.exe"
	startInfo.Arguments = String.Format("/A ""page={0}""{1}", page, document)
	myProcess.StartInfo = startInfo
	myProcess.Start()
End Sub