1

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47

4 Answers4

3
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");
podcast
  • 131
  • 7
1

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.

Zj Wine
  • 125
  • 12
0

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0
  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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xeon_Flux
  • 9
  • 3
  • That is completely irrelevant to the question. This kind of issue is generally that _after_ you create something and save it as a file, you want to give the user the ability to open a file browser and jump to that newly created file so they can use it for something. – Nyerguds May 29 '16 at 16:57