如何在MonoDevelop gtk#中打开文件位置或打开文件夹位置?

本文关键字:位置 文件夹 文件 gtk# MonoDevelop | 更新日期: 2023-09-27 17:53:20

我想打开一个文件的位置,并在Mac上的资源管理器中选择文件,Ubuntu从MonoDevelop。

这段代码可以在Windows上运行(但不能在Mac和Ubuntu上运行):

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

如何在MonoDevelop gtk#中打开文件位置或打开文件夹位置?

Dim dir_path As String = "/media/os/test"
' Windows path example: dir_path = "C:'test"
Process.Start("file://" & dir_path)

在Ubuntu和Windows XP上测试并运行。

来源:http://www.stevenbrown.ca/blog/archives/156


到2020-10,在mono 6.10中,上述方法在Ubuntu 20.04上不起作用。下面的方法解决了这个问题。

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

你可以在Mac上使用'打开',像这样

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

这里-R表示显示,在Finder中选择而不是打开。要查找open的更多用法,只需在terminal中输入open

使用Process.Start(),您可以绕过。net框架,进入您正在运行的平台,执行任意进程。

在Windows上你想打开Windows资源管理器,在Mac上你想打开Finder,在Ubuntu上它被简单地称为文件浏览器。

框架中没有Environment.OpenFileBrowser(string path)方法,因此将不得不让您的程序确定它在哪个平台上运行,并打开适当的文件查看器。

参见如何在运行时检查操作系统版本,例如windows或linux,而不使用条件编译语句来执行前者

  1. 您正在调用特定于操作系统(Windows)的方法。

  2. 在函数/方法中尝试以下操作:

    示例- 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 ();
            }
        }
    }
    

文件现在已经被复制到一个可编辑(默认)或只读(在属性pad中设置)的textviewer Gtk小部件中。从那里,你应该可以按照你的选择来操作它。