Detecting OS ? (mono/.NET)

  • Thread starter Thread starter Christoffer Soerensen
  • Start date Start date
C

Christoffer Soerensen

Hi,

I am currently building an application in C# using mono and .NET. I
want to get a MIME type for a specific file, which is why I want to
detect the OS on which the application is running. On Windows, I would
look up the registry, on Linux/OSX/etc. I would look in
/etc/mime-types (or where the mime types are located). How would I
detect the OS ?

I have tried the following:

OperatingSystem os = Environment.OSVersion;
Version vs = os.Version;

This gives me: "5.1.2600" on Linux :-)

Maybe it is better to build a MIME database on my own instead of
relying on the OS ?

Regards,

Christoffer
 
I don't have a lot of details on the underlying implementation of Mono (just
because I haven't downloaded all the source).
However, I have a feeling that for "interoperability reasons", they fool
your code into believing you are running under windows. I can see where that
might come in handy, deceitful as it may be :-)

Perhaps you can try to use the DllImport tag to write a p/invoke function
call to a Windows dll (like perhaps one of the registry API functions). Wrap
this up in a Try-Catch, and if the function fails (because the DLL doesn't
exist), then you are not in Windows. The only catch may be if the system is
using Wine or some windows emulator on Linux.

-Rob Teixeira [MVP]
 
OperatingSystem os = Environment.OSVersion;
MessageBox.Show("OS:" + os.Platform + "\nVer:" + os.Version +
"\nToString:" + os.ToString());

(e-mail address removed) (Christoffer Soerensen) wrote in
 
This gives me: "5.1.2600" on Linux :-)

You are having this problem because Mono is simulating a Windows XP
environment.
 
thats the version number...and is the same as what i get on xp pro so it
doesnt look good, but what does the platform property return?

im omw to install the runtime on my mandrake box tonight...
 
chris said:
OperatingSystem os = Environment.OSVersion;
MessageBox.Show("OS:" + os.Platform + "\nVer:" + os.Version +
"\nToString:" + os.ToString());

Thank you!

Output from my C# program using Mono:

"Version: 5.1.2600.0
Platform: 128
ToString: Unix 5.1.2600.0"

I haven't tried using MS .NET but probably the ToString string is
"Windows 5.1.2600.0" on XP.

Thanks for all suggestions.

Christoffer
 
Back
Top