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

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

Sample C#

private static void ShowFileInExplorer(string filePath)
{
	try
	{
		var winDir = Environment.GetEnvironmentVariable("windir");
		if (winDir != null)
		{
			var explorerPath = Path.Combine(winDir, @"explorer.exe");
			var arguments = String.Format("/select, {0}{1}{0}", (char)34, filePath);
			Process.Start(explorerPath, arguments);
		}
	}
	catch (Exception ex)
	{
		//handle the exception your way!
	}
}

Sample VB.NET

Private Shared Sub ShowFileInExplorer(filePath As String)
	Try
		Dim winDir = Environment.GetEnvironmentVariable("windir")
		If winDir IsNot Nothing Then
			Dim explorerPath = Path.Combine(winDir, "explorer.exe")
			Dim arguments = [String].Format("/select, {0}{1}{0}", Char(34), filePath)
			Process.Start(explorerPath, arguments)
		End If
	Catch ex As Exception
	        'handle the exception your way!
        End Try
End Sub

How to show and select a file in explorer using VBA

To show and select a file in explorer using VBA you can use the following snippet.
This snippet should also work in VB6.

Sample VBA

Dim path As String
Dim explorerPath As String
Dim cmd As String

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

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)