简体   繁体   English

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

[英]How can I open a file location or open a folder location in MonoDevelop Gtk#?

I want to open a file's location and select the file in explorer on Mac, Ubuntu from MonoDevelop.我想打开一个文件的位置并在 Mac 上的资源管理器中选择文件,MonoDevelop 的 Ubuntu。

This code is working on Windows (but it is not working on Mac and Ubuntu):此代码适用于 Windows(但不适用于 Mac 和 Ubuntu):

System.Diagnostics.Process.Start("explorer.exe", "/select, " + fileaddress);
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.在 Ubuntu 和 Windows XP 上测试和工作。

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


By 2020-10, in mono 6.10, the above method didn't work on Ubuntu 20.04.到 2020-10 年,在 mono 6.10 中,上述方法在 Ubuntu 20.04 上不起作用。 The below approach solved the problem.下面的方法解决了这个问题。

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

Using Process.Start() you bypass the .NET framework and move into the platform you're running onto, executing an arbitrary process.使用Process.Start()可以绕过 .NET 框架并进入您正在运行的平台,执行任意进程。

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.在 Windows 上你想打开 Windows 资源管理器,在 Mac 上你想打开 Finder,在 Ubuntu 上它被简单地称为文件浏览器。

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

See How to check the OS version at runtime eg windows or linux without using a conditional compilation statement to perform the former.请参阅如何在运行时检查操作系统版本,例如 windows 或 linux,而不使用条件编译语句来执行前者。

  1. You are calling an OS specific (Windows) method.您正在调用特定于操作系统的 (Windows) 方法。 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.该文件现已复制到可编辑(默认)或只读(在属性板中设置)textviewer Gtk 小部件中。 From there you should be able to manipulate it as you so choose.从那里你应该能够按照你的选择来操纵它。

You can use ' open ' on Mac, like this你可以在 Mac 上使用 ‘ open ’,就像这样

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

Here -R means reveal, to select in the Finder instead of opening.这里 -R 表示显示,在 Finder 中选择而不是打开。 To find more usage for open , just type open in terminal.要查找open 的更多用法,只需在终端中键入open

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM