【问题标题】:How can I open a file location or open a folder location in MonoDevelop Gtk#?如何在 MonoDevelop Gtk# 中打开文件位置或打开文件夹位置?
【发布时间】:2014-05-03 11:10:59
【问题描述】:

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

此代码适用于 Windows(但不适用于 Mac 和 Ubuntu):

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

【问题讨论】:

    标签: c# monodevelop gtk#


    【解决方案1】:

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

    在 Windows 上您要打开 Windows 资源管理器,在 Mac 上您要打开 Finder,在 Ubuntu 上它简称为文件浏览器。

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

    查看How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement 执行前者。

    【讨论】:

    • 如何找到示例?
    • 我不知道,尝试在网上搜索类似“osx command line to start finder”的内容。
    • 这个答案可能更全面(可操作)。
    【解决方案2】:
    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");
    

    【讨论】:

      【解决方案3】:
      1. 您正在调用特定于操作系统 (Windows) 的方法。这不会跨平台工作。

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

        示例 - 内部点击事件:

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

      该文件现在已被复制到可编辑(默认)或只读(在属性面板中设置)textviewer Gtk 小部件中。从那里您应该能够根据自己的选择对其进行操作。

      【讨论】:

      • 这与问题完全无关。这种问题通常是在你创建一些东西并将它保存为一个文件之后,你想让用户能够打开一个文件浏览器并跳转到那个新创建的文件以便他们可以使用它为某事。
      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2022-07-21
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 2021-08-17
        相关资源
        最近更新 更多