Recently, I am working on a. Net cross-platform tool. I need to make a judgment on the operating system on which the client runs in the program.
The. net api provides the system. environment. osversion encapsulation to obtain information about the operating system.
So I made a small program to test the information returned by each platform. Of course, Mono is used as the middle layer for cross-platform deployment.
The version I used is mono 2.6.7.
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace osinfotest <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> string osinfo = system. environment. osversion. versionstring; <br/> console. writeline ("versionstring: {0}", osinfo); </P> <p> string platform = system. environment. osversion. platform. tostring (); <br/> console. writeline ("Platform: {0}", platform); </P> <p> console. readline (); <br/>}< br/>}
Deploy the compiled osinfotest.exe to MacOS and Linux Red Hat respectively.
Running result of MACOs:
Linux running result:
For example, if the red part is marked, platform is UNIX!
System. environment. osversion. Platform returns an enumeration of platformid, which defines MacOSX.
After a query, we found that this was a mono BUG: [mono-bugs] [bug 515570].
I found the cause, but the bug has not been corrected. I still need to solve the problem myself:
However, we can use the console information of MACOs for judgment.
After the code is modified, the isrunningmacos and getmacosinfo methods are added.
One method is to directly determine whether the running OS is Mac, and the other is to call the Mac console to obtain the current OS version information.
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. diagnostics; <br/> using system. runtime. interopservices; </P> <p> namespace osinfotest <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> bool isruningmac = isrunningmacos (); </P> <p> string osinfo = ""; <br/> If (isruningmac) <br/> osinfo = getmacosin Fo (); <br/> else <br/> osinfo = system. environment. osversion. versionstring; </P> <p> console. writeline ("versionstring: {0}", osinfo); </P> <p> string platform = ""; <br/> If (isruningmac) <br/> platform = "MacOSX"; <br/> else <br/> platform = system. environment. osversion. platform. tostring (); </P> <p> console. writeline ("Platform: {0}", platform); </P> <p> console. readline (); <br/>}</P> <p> [dllimport ("Li BC ")] <br/> static extern int uname (intptr BUF); </P> <p> static bool isrunningmacos () <br/>{< br/> intptr Buf = intptr. zero; <br/> try <br/> {<br/> Buf = marshal. allochglobal (8192); <br/> If (uname (BUF) = 0) <br/> {<br/> string OS = marshal. ptrtostringansi (BUF); <br/> If (OS = "Darwin") <br/> return true; <br/>}< br/> catch <br/>{}< br/> finally <br/>{< br/> If (BUF! = Intptr. zero) <br/> marshal. freehglobal (BUF); <br/>}</P> <p> return false; <br/>}</P> <p> static string getmacosinfo () <br/>{< br/> string macosinfo; <br/> try <br/>{< br/> process getmacosinfoprocess = new process (); <br/> getmacosinfoprocess. startinfo. filename = "sw_vers"; <br/> getmacosinfoprocess. startinfo. arguments = ""; <br/> getmacosinfoprocess. startinfo. useshellexecute = false; <br/> getmacosinfoprocess. startinfo. redirectstandardoutput = true; <br/> getmacosinfoprocess. start (); </P> <p> getmacosinfoprocess. waitforexit (); </P> <p> If (getmacosinfoprocess. exitcode = 0) <br/>{< br/> macosinfo = getmacosinfoprocess. standardoutput. readtoend (); <br/> macosinfo = macosinfo. replace ('/N', ''); <br/> macosinfo = macosinfo. replace ("productname:/T", ""); <br/> macosinfo = macosinfo. replace ("productversion:/T", ""); <br/> macosinfo = macosinfo. replace ("buildversion:/T", ""); <br/>}< br/> else <br/>{< br/> macosinfo = ""; <br/>}< br/> catch <br/> {<br/> macosinfo = ""; <br/>}< br/> return macosinfo; <br/>}< br/>}
Okay. Run it again:
Compare the result of running "sw_vers" directly on MACOs:
OK ~ Done.