[.net] Cross platform shell execute
I've been searching for a while for a way to open a folder or open a file with the default program file in a cross-platform manner. At this point, I can create a process on Windows with UseShellExecute set to true or use the "open" command on Linux or OS X to open a file. On Windows, this same technique works for a folder path, but not on Linux.
Is there some command on Linux to open a folder for display in the default file browser that won't be tied to GNOME or KDE specifically? Even better, has anyone found a way to do this all in a truly cross-platform manner?
Quote:Is there some command on Linux to open a folder for display in the default file browser that won't be tied to GNOME or KDE specifically? Even better, has anyone found a way to do this all in a truly cross-platform manner?
Nope.
For those interested, here's some code I put together. It's a bit of a hack, though...
public void DisplayFolder(string path){ bool osFound = false; ArrayList processes = new ArrayList(); IDictionary vars = System.Environment.GetEnvironmentVariables(); //Try Windows string osType = System.Environment.GetEnvironmentVariable("OS"); if (osType != null) { if (Regex.IsMatch(osType, "Win", RegexOptions.IgnoreCase)) { processes.Add( new ProcessAndArgs(path, "", true) ); osFound = true; } } //Try Linux if (!osFound) { if (vars.Contains("GNOME_DESKTOP_SESSION_ID")) { processes.Add( new ProcessAndArgs("nautilus", "--nodesktop --browser " + path) ); osFound = true; } else if (vars.Contains("KDEDIR")) { processes.Add( new ProcessAndArgs("kfmclient", "exec " + path) ); osFound = true; } } //Assume Mac OS X at this point if (!osFound) { processes.Add( new ProcessAndArgs("open", path) ); } foreach(ProcessAndArgs startInfo in processes) { try { Process newProcess = new Process(); newProcess.StartInfo.FileName = startInfo.FileName; newProcess.StartInfo.Arguments = startInfo.Args; newProcess.StartInfo.UseShellExecute = startInfo.ShellExecute; bool success = newProcess.Start(); if (success) { break; } } catch { //Do nothing } }}public class ProcessAndArgs{ public string FileName = "", Args = ""; public bool ShellExecute = false; public ProcessAndArgs(string fileName, string args) { FileName = fileName; Args = args; } public ProcessAndArgs(string fileName, string args, bool shellExecute) { FileName = fileName; Args = args; ShellExecute = shellExecute; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement