【发布时间】:2014-05-03 11:10:59
【问题描述】:
我想打开文件的位置并在 Mac 上的资源管理器中选择文件,Ubuntu 来自 MonoDevelop。
此代码适用于 Windows(但不适用于 Mac 和 Ubuntu):
System.Diagnostics.Process.Start("explorer.exe", "/select, " + fileaddress);
【问题讨论】:
标签: c# monodevelop gtk#
我想打开文件的位置并在 Mac 上的资源管理器中选择文件,Ubuntu 来自 MonoDevelop。
此代码适用于 Windows(但不适用于 Mac 和 Ubuntu):
System.Diagnostics.Process.Start("explorer.exe", "/select, " + fileaddress);
【问题讨论】:
标签: c# monodevelop gtk#
使用Process.Start(),您可以绕过.NET 框架并进入您正在运行的平台,执行任意进程。
在 Windows 上您要打开 Windows 资源管理器,在 Mac 上您要打开 Finder,在 Ubuntu 上它简称为文件浏览器。
框架中没有Environment.OpenFileBrowser(string path) 方法,因此您必须让您的程序确定它在哪个平台上运行,并打开适当的文件查看器。
【讨论】:
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");
【讨论】:
您正在调用特定于操作系统 (Windows) 的方法。这不会跨平台工作。
在函数/方法中尝试以下操作:
示例 - 内部点击事件:
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 小部件中。从那里您应该能够根据自己的选择对其进行操作。
【讨论】:
你可以在 Mac 上使用 'open',像这样
System.Diagnostics.Process.Start("open", $"-R \"{File_Path_You_Wanna_Select}\"");
这里 -R 表示显示,在 Finder 中选择而不是打开。 要了解 open 的更多用法,只需在终端中输入 open。
【讨论】: