【发布时间】:2016-06-06 14:15:22
【问题描述】:
搜索 stackoverflow 和谷歌这个问题只在 Windows 和 .NET 的上下文中得到了回答,而不是在 OSX 上的 Unity3D 中使用的 Mono 版本。无论出于何种原因,我在 Unity3D 的 OSX 上的 Mono 中都没有找到任何解决方案或答案。有可能我正在做一些非常愚蠢的事情,或者 Unity3D 的 Mono+OSX 可能从根本上破坏了某些东西。所以这个问题
我正在尝试使用 C#、Mono(Unity3D)、OSX 执行一个简单的命令。为了简单起见,这里是来自 shell 的命令
/bin/sh -c "echo \"hello world\""
这行得通。这是我的 C# 代码
// original working line
// /bin/sh -c "echo \"hello world\""
string args = @"-c ""echo \""hello world\""""";
var proc = new System.Diagnostics.Process {
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "/bin/sh",
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
},
};
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
result += proc.StandardError.ReadToEnd();
Debug.Log(proc.ExitCode);
Debug.Log(result);
失败了
Win32Exception: ApplicationName='/bin/sh', CommandLine='-c "echo \"hello world\""', CurrentDirectory=''
您可以看到参数(即CommandLine=)似乎是正确的
删除内引号有效
string args = @"-c ""echo hello world""";
当然,我的实际用例最终需要引号。
如何在 Mono(Unity3D) OSX 上将引号传递给 Process.Start?
更新
事实证明,这是 Unity3D 中 Mono 特有的某种问题。如果在 Xamarin Studio 中运行相同的代码,它会按预期工作。
仍在寻找可能的解决方法。
【问题讨论】:
-
我的声誉是 21k 加上也许你会让我怀疑我已经为此工作了 6 个小时。 Mono+OSX+C# 没有像 .NET on Window 那样被很好地覆盖。
-
顺便说一句,您的代码在 centos 上对我来说可以正常工作。令人惊讶的是它在 OSX 上的任何不同。
-
见上文。这显然是 Unity 中使用的单声道的问题。在 Xamarin Studio 中运行良好