title: Preprocessor : Get Operating System .Net Core
body: <p>I'm writing a class that I wish to use on both Windows &amp; Linux.<br><br>
  One of the methods that is in this class is accessing the <a href="https://en.wikipedia.org/wiki/Windows_Registry" rel="nofollow noreferrer">Windows Registry</a>
  <br><br>What I'm hoping to achieve is to somehow disable this particular method from being used when using a Linux machine.<br><br>First of all I did some research to see if there was something for <a href="https://en.wikipedia.org/wiki/.NET_Core" rel="nofollow noreferrer">.Net Core</a> that would allow me to check which operating system is in use, I found <a href="https://stackoverflow.com/questions/38790802/determine-operating-system-in-net-core">this</a> and sure enough that works.<br><br>When I implemented it into my code when accessing a method, I was hoping to disable the method that's accessing the windows registry, however the closest I could get to this was using a switch statement, something like this</p>
  
  <pre><code>switch (OS)
  {
      case OSX:
      return;
  
      case LINUX:
      return
  }
  </code></pre>
  
  <p>To <code>return</code> if the operating system was not supported, this worked, however I then thought disabling it from being accessed all together would be much better rather than throwing an error for an operating system thats not supported for that particular method<br><br>I then went on to look at <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/" rel="nofollow noreferrer">preprocessor directives</a> thinking that if I'm able to detect and disable parts of code depending the frameworks etc, maybe I could use something like this to disable parts of code depending on the operating system that way they could never be called even when trying to access the method<br><br>I went on from there to see if I could disable parts of code using <code>preprocessor directives</code>.<br>I found <a href="https://stackoverflow.com/questions/11726665/simple-ways-to-disable-parts-of-code">this</a>.<br><br>I understand that it is for <a href="https://en.wikipedia.org/wiki/C%2B%2B" rel="nofollow noreferrer">C++</a> however it seems to be the closest I could find for what I'm trying to achieve within <code>.Net Core</code> <br><br>In a perfect world, it would look something like this</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Get the file mime type
      /// &lt;/summary&gt;
      /// &lt;param name="filePathLocation"&gt;file path location&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      `#if WINDOWS`
      public static string GetMimeType(this string filePathLocation)
      {
          if (filePathLocation.IsValidFilePath())
          {
              string mimeType = "application/unknown";
              string ext = Path.GetExtension(filePathLocation).ToLower();
              Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
  
              if (regKey != null &amp;&amp; regKey.GetValue("Content Type") != null)
              {
                  mimeType = regKey.GetValue("Content Type").ToString();
              }
              return mimeType;
          }
          return null;
      }
  `#endif`
  </code></pre>
  
  <p>I did see <code>#Define</code> so I tried something like this <code>#define IS_WINDOWS</code> and added it to my class along with <code>#if IS_WINDOWS</code>
  however, I couldn't see how to change that value if I'm hoping to just reuse the static class over and over.</p>