Frage

I want to open a file's location and select the file in explorer on Mac, Ubuntu from MonoDevelop.

This code is working on Windows (but it is not working on Mac and Ubuntu):

System.Diagnostics.Process.Start("explorer.exe", "/select, " + fileaddress);
War es hilfreich?

Lösung 3

Using Process.Start() you bypass the .NET framework and move into the platform you're running onto, executing an arbitrary process.

On Windows you want to open the Windows Explorer, on Mac you want to open Finder and on Ubuntu it's simply called File Browser.

There is no Environment.OpenFileBrowser(string path) method in the framework, so you will have to let your program determine which platform it is running on, and open the approperiate file viewer.

See How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement to perform the former.

Andere Tipps

Dim dir_path As String = "/media/os/test"
' Windows path example: dir_path = "C:\test"

Process.Start("file://" & dir_path)

Tested and worked on Ubuntu and Windows XP.

Source: http://www.stevenbrown.ca/blog/archives/156


By 2020-10, in mono 6.10, the above method didn't work on Ubuntu 20.04. The below approach solved the problem.

System.Diagnostics.Process.Start("mimeopen", "/var/tmp");

You can use 'open' on Mac, like this

System.Diagnostics.Process.Start("open", $"-R \"{File_Path_You_Wanna_Select}\"");

Here -R means reveal, to select in the Finder instead of opening. To find more usage for open, just type open in terminal.

  1. You are calling an OS specific (Windows) method. That won't work cross-platform.

  2. Try the following inside a function/method:

    Example - inside click event:

    protected void OnOpen (object sender, EventArgs e)
    {
        using(FileChooserDialog chooser =
            new FileChooserDialog(null,
                                  "Select document to open...",
                                  null,
                                  FileChooserAction.Open,
                                  "Open Selected File",
                                  ResponseType.Accept,
                                  "Discard & Return to Main Page",
                                  ResponseType.Cancel))
        {
            if (chooser.Run () == (int)ResponseType.Accept)
            {
                System.IO.StreamReader file = System.IO.File.OpenText (chooser.Filename);
    
                /* Copy the contents to editableTxtView   <- This is the Widget Name */
                editableTxtView.Buffer.Text = file.ReadToEnd ();
    
                /* If you want to read the file into explorer, thunar, Notepad, etc.,
                 * you'll have to research that yourself.  */
    
                //Close file - - KEEP IT CLEAN - - & deAllocated memory!!
                file.Close ();
            }
        }
    }
    

The file has now been copied into an editable (Default) or read only (set in properties pad) textviewer Gtk widget. From there you should be able to manipulate it as you so choose.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top