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

Please Share us:
Please rate this snippet

One thought on “How to show and select a file in explorer using C# and VB.NET

Leave a Reply