[Mono-osx] OSX vs Linux
Gavin Landon
Gavin.Landon at ignitetech.com
Tue Dec 18 12:54:35 EST 2007
Thanks for the help.. This is what I came out with that works out side
of the WaitForExit, which doesn't work on OSX or Linux, but I'll
continue to work on that..
-----------------------
using System.Runtime.InteropServices; //mono referenced
using Mono.Unix;
static public void RunIt(string sFile, string sParm, bool bWaitForExit)
{
//works with URL's, Executables, and File Associations for all
three platforms.
bool bUseShellExecute = true;
try
{
string os = OSInfo.DetectOS();
if(os=="OSX")
{
sParm = "-t " + sFile;
sFile = "open";
bUseShellExecute = false;
}
if(os=="Linux")
{
sParm = sFile;
sFile = "xdg-open";
bUseShellExecute = false;
}
Process proc = new Process();
proc.StartInfo.FileName = sFile;
proc.StartInfo.Arguments = sParm;
proc.StartInfo.UseShellExecute = bUseShellExecute;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
if(bWaitForExit)
proc.WaitForExit();
} catch (Exception ec) {
Common.LogError("Error, \"" + ec.Message.ToString() +
"\" while running " + sFile + ".\n\n"+ec.ToString());
}
}
[DllImport ("libc")]
static extern int uname (IntPtr buf);
static public string DetectOS ()
{
string style;
if (!IsUnix) {
return "Windows";
}
IntPtr buf = UnixMarshal.AllocHeap(8192);
if (uname (buf) != 0){
return "Warning: Unable to detect OS";
}
string os = Marshal.PtrToStringAnsi (buf);
if (os == "Darwin")
style = "OSX";
else
style = os;
UnixMarshal.FreeHeap(buf);
return style;
}
static bool IsUnix {
get {
int p = (int) Environment.OSVersion.Platform;
return ((p == 4) || (p == 128));
}
}
More information about the Mono-osx
mailing list