How to Implement "Show in Explorer"

I am working on a project and some editor tools and I would like to have the ability to have the user click a button to Show the item in the Explorer window. Or have the tool auto open a folder after it is done processing what it needs to do. I have looked around and Google this, but it looks like every link is to some bug related to Show in Explorer. Not how to implement it.

I needed this functionality today as well. As it turned out there is a very easy solution:

EditorUtility.RevealInFinder(path)

nahoy mentioned that it works for Mac, I can confirm that it works on Windows, too. No need to work with System.Diagnostics.Process.

The command line you want is:

explorer.exe /select,<path to file or folder>

You can run a command line such as this with System.Diagnostics.Process.Start, as follows (C#) ...

public void ShowExplorer(string itemPath)
{
    itemPath = itemPath.Replace(@"/", @"\");   // explorer doesn't like front slashes
    System.Diagnostics.Process.Start("explorer.exe", "/select,"+itemPath);
}

I DID IT! ! ! SO EASY HARD TO BELIEVE!

Application.OpenURL("file://[dir]");

For some reason, this opens the actual file explorer instead of the browser one. FINALLY!

Here’s how to do it for both Mac and Windows. There is currently no platform defines for the current OS that you are in, so my code tries both Windows Explorer and Mac Finder, and just skips any error. So OpenInFileBrowser is the cross-platform convenience function that will work in both OS’s.

My code will also automatically select/highlight the file (if the argument passed is a file), or open the contents of a folder (if the argument passed is a folder).

I put this code in public domain, feel free to use it!

	public static void OpenInMacFileBrowser(string path)
	{
		bool openInsidesOfFolder = false;

		// try mac
		string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes

		if (Directory.Exists(macPath)) // if path requested is a folder, automatically open insides of that folder
		{
			openInsidesOfFolder = true;
		}

		//Debug.Log("macPath: " + macPath);
		//Debug.Log("openInsidesOfFolder: " + openInsidesOfFolder);

		if (!macPath.StartsWith("\""))
		{
			macPath = "\"" + macPath;
		}
		if (!macPath.EndsWith("\""))
		{
			macPath = macPath + "\"";
		}
		string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
		//Debug.Log("arguments: " + arguments);
		try
		{
			System.Diagnostics.Process.Start("open", arguments);
		}
		catch(System.ComponentModel.Win32Exception e)
		{
			// tried to open mac finder in windows
			// just silently skip error
			// we currently have no platform define for the current OS we are in, so we resort to this
			e.HelpLink = ""; // do anything with this variable to silence warning about not using it
		}
	}

	public static void OpenInWinFileBrowser(string path)
	{
		bool openInsidesOfFolder = false;

		// try windows
		string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes

		if (Directory.Exists(winPath)) // if path requested is a folder, automatically open insides of that folder
		{
			openInsidesOfFolder = true;
		}
		try
		{
			System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
		}
		catch(System.ComponentModel.Win32Exception e)
		{
			// tried to open win explorer in mac
			// just silently skip error
			// we currently have no platform define for the current OS we are in, so we resort to this
			e.HelpLink = ""; // do anything with this variable to silence warning about not using it
		}
	}

	public static void OpenInFileBrowser(string path)
	{
		OpenInWinFileBrowser(path);
		OpenInMacFileBrowser(path);
	}