- Home /
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.
Answer by yoyo · Jan 18, 2011 at 07:22 AM
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);
}
Note that this will only work under Windows, not on the $$anonymous$$ac.
For mac you can just do : EditorUtility.RevealInFinder(path)
Answer by coeing · Nov 18, 2014 at 10:01 AM
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.
Yup it works, too easy~! To be added, Application.dataPath return the path of Assets folder, so together it is: (e.g. Resources folder)
EditorUtility.RevealInFinder(Application.dataPath + "/Rescources/")
This is the best answer and should be marked as the answer for this question.
Unfortunately, this only works in the Editor. Which maybe it's what the OP asked, but many other people will need to do that at runtime…
This works great, but it reveals the last component of the path in finder/explorer. That is, if you pass in a specific directory, it takes you to that directory's parent and highlights the specified directory.
$$anonymous$$y solution is to pass in the first file in the directory if one exists:
var path = Path.Combine(Application.dataPath, "Resources");
var file = Directory.EnumerateFiles(path).FirstOrDefault();
if (!string.IsNullOrEmpty(file))
EditorUtility.RevealInFinder(Path.Combine(path, file));
else
EditorUtility.RevealInFinder(path);
Obviously the downfall here is if there is no file in the directory, in which case it falls back to the original behaviour...
Answer by nathanthesnooper · Oct 21, 2016 at 09:33 PM
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!
Answer by AnomalusUndrdog · May 09, 2013 at 04:45 AM
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);
}
Can use Application.platform to check if it is Win or $$anonymous$$ac
Shorter version that should also work on Windows and $$anonymous$$ac:
string path = path.TrimEnd(new[]{'\\', '/'}); // $$anonymous$$ac doesn't like trailing slash
Process.Start(path);
Your answer
Follow this Question
Related Questions
Dynamically update the editor? 3 Answers
Ask for Script Compilation from EditorWindow Button 1 Answer
event sequencing 1 Answer
Select and deselect in Editor via C# 1 Answer
Input in editor scripts 2 Answers