Finding a MIME type for a file on windows










12















Is there a way to get a file's MIME type using some system call on Windows? I'm writing an IIS extension in C++, so it must be callable from C++, and I do have access to IIS if there is some functionality exposed. Obviously, IIS itself must be able to do this, but my googling has been unable to find out how. I did find this .net related question here on SO, but that doesn't give me much hope (as neither a good solution nor a C++ solution is mentioned there).



I need it so I can serve up dynamic files using the appropriate content type from my app. My plan is to first consult a list of MIME types within my app, then fall back to the system's MIME type listing (however that works; obviously it exists since it's how you associate files with programs). I only have a file extension to work with in some cases, but in other cases I may have an actual on-disk file to examine. Since these will not be user-uploaded files, I believe I can trust the extension and I'd prefer an extension-only lookup solution since it seems simpler and faster. Thanks!










share|improve this question
























  • Your assumption is incorrect: on Windows, the association between extensions and programs does not use MIME types. In the registry, each extension is associated with a descriptive entry, and that has a ShellOpenCommand value. This indirection allows two extensions to share one descriptive entry, e.g. .jpeg and .jpg share the descriptive entry jpegfile

    – MSalters
    Mar 31 '10 at 13:07












  • MSalters, I could swear that in XP, the file association dialog had a place in it for MIME type... but here on Win Server 2k8, in the "Default Programs" dialog, it seems to be just as you say... that is not useful to me :(

    – rmeador
    Mar 31 '10 at 15:27















12















Is there a way to get a file's MIME type using some system call on Windows? I'm writing an IIS extension in C++, so it must be callable from C++, and I do have access to IIS if there is some functionality exposed. Obviously, IIS itself must be able to do this, but my googling has been unable to find out how. I did find this .net related question here on SO, but that doesn't give me much hope (as neither a good solution nor a C++ solution is mentioned there).



I need it so I can serve up dynamic files using the appropriate content type from my app. My plan is to first consult a list of MIME types within my app, then fall back to the system's MIME type listing (however that works; obviously it exists since it's how you associate files with programs). I only have a file extension to work with in some cases, but in other cases I may have an actual on-disk file to examine. Since these will not be user-uploaded files, I believe I can trust the extension and I'd prefer an extension-only lookup solution since it seems simpler and faster. Thanks!










share|improve this question
























  • Your assumption is incorrect: on Windows, the association between extensions and programs does not use MIME types. In the registry, each extension is associated with a descriptive entry, and that has a ShellOpenCommand value. This indirection allows two extensions to share one descriptive entry, e.g. .jpeg and .jpg share the descriptive entry jpegfile

    – MSalters
    Mar 31 '10 at 13:07












  • MSalters, I could swear that in XP, the file association dialog had a place in it for MIME type... but here on Win Server 2k8, in the "Default Programs" dialog, it seems to be just as you say... that is not useful to me :(

    – rmeador
    Mar 31 '10 at 15:27













12












12








12


2






Is there a way to get a file's MIME type using some system call on Windows? I'm writing an IIS extension in C++, so it must be callable from C++, and I do have access to IIS if there is some functionality exposed. Obviously, IIS itself must be able to do this, but my googling has been unable to find out how. I did find this .net related question here on SO, but that doesn't give me much hope (as neither a good solution nor a C++ solution is mentioned there).



I need it so I can serve up dynamic files using the appropriate content type from my app. My plan is to first consult a list of MIME types within my app, then fall back to the system's MIME type listing (however that works; obviously it exists since it's how you associate files with programs). I only have a file extension to work with in some cases, but in other cases I may have an actual on-disk file to examine. Since these will not be user-uploaded files, I believe I can trust the extension and I'd prefer an extension-only lookup solution since it seems simpler and faster. Thanks!










share|improve this question
















Is there a way to get a file's MIME type using some system call on Windows? I'm writing an IIS extension in C++, so it must be callable from C++, and I do have access to IIS if there is some functionality exposed. Obviously, IIS itself must be able to do this, but my googling has been unable to find out how. I did find this .net related question here on SO, but that doesn't give me much hope (as neither a good solution nor a C++ solution is mentioned there).



I need it so I can serve up dynamic files using the appropriate content type from my app. My plan is to first consult a list of MIME types within my app, then fall back to the system's MIME type listing (however that works; obviously it exists since it's how you associate files with programs). I only have a file extension to work with in some cases, but in other cases I may have an actual on-disk file to examine. Since these will not be user-uploaded files, I believe I can trust the extension and I'd prefer an extension-only lookup solution since it seems simpler and faster. Thanks!







c++ windows mime-types






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 11:46









Community

11




11










asked Mar 30 '10 at 21:52









rmeadorrmeador

19.5k145188




19.5k145188












  • Your assumption is incorrect: on Windows, the association between extensions and programs does not use MIME types. In the registry, each extension is associated with a descriptive entry, and that has a ShellOpenCommand value. This indirection allows two extensions to share one descriptive entry, e.g. .jpeg and .jpg share the descriptive entry jpegfile

    – MSalters
    Mar 31 '10 at 13:07












  • MSalters, I could swear that in XP, the file association dialog had a place in it for MIME type... but here on Win Server 2k8, in the "Default Programs" dialog, it seems to be just as you say... that is not useful to me :(

    – rmeador
    Mar 31 '10 at 15:27

















  • Your assumption is incorrect: on Windows, the association between extensions and programs does not use MIME types. In the registry, each extension is associated with a descriptive entry, and that has a ShellOpenCommand value. This indirection allows two extensions to share one descriptive entry, e.g. .jpeg and .jpg share the descriptive entry jpegfile

    – MSalters
    Mar 31 '10 at 13:07












  • MSalters, I could swear that in XP, the file association dialog had a place in it for MIME type... but here on Win Server 2k8, in the "Default Programs" dialog, it seems to be just as you say... that is not useful to me :(

    – rmeador
    Mar 31 '10 at 15:27
















Your assumption is incorrect: on Windows, the association between extensions and programs does not use MIME types. In the registry, each extension is associated with a descriptive entry, and that has a ShellOpenCommand value. This indirection allows two extensions to share one descriptive entry, e.g. .jpeg and .jpg share the descriptive entry jpegfile

– MSalters
Mar 31 '10 at 13:07






Your assumption is incorrect: on Windows, the association between extensions and programs does not use MIME types. In the registry, each extension is associated with a descriptive entry, and that has a ShellOpenCommand value. This indirection allows two extensions to share one descriptive entry, e.g. .jpeg and .jpg share the descriptive entry jpegfile

– MSalters
Mar 31 '10 at 13:07














MSalters, I could swear that in XP, the file association dialog had a place in it for MIME type... but here on Win Server 2k8, in the "Default Programs" dialog, it seems to be just as you say... that is not useful to me :(

– rmeador
Mar 31 '10 at 15:27





MSalters, I could swear that in XP, the file association dialog had a place in it for MIME type... but here on Win Server 2k8, in the "Default Programs" dialog, it seems to be just as you say... that is not useful to me :(

– rmeador
Mar 31 '10 at 15:27












3 Answers
3






active

oldest

votes


















13














HKCR\.<ext>Content Type (where "ext" is the file extension) will normally hold the MIME type.






share|improve this answer























  • Unlike Avitus solution, this supports both .jpg and .jpeg

    – MSalters
    Mar 31 '10 at 12:59


















0














Pasted from http://www.snoyman.com/blog/2012/03/ie-mimetype-png.html:



#include <urlmon.h>
#include <stdio.h>

int main(int argc, char* argv)

char buff[256];
LPWSTR out;

FILE *in = fopen("title.png", "rb");

fread(buff, 1, 256, in);

FindMimeFromData(NULL, NULL, buff, 256, NULL, FMFD_DEFAULT, &out, 0);

printf("%lsn", out);

return 0;






share|improve this answer






























    0














    In Windows 10, the different MIME types are stored in the registry at:



    HKEY_CLASSES_ROOTMIMEDatabaseContent Type


    with a key for each content type (e. g. text/plain) under that key.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2549015%2ffinding-a-mime-type-for-a-file-on-windows%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      HKCR\.<ext>Content Type (where "ext" is the file extension) will normally hold the MIME type.






      share|improve this answer























      • Unlike Avitus solution, this supports both .jpg and .jpeg

        – MSalters
        Mar 31 '10 at 12:59















      13














      HKCR\.<ext>Content Type (where "ext" is the file extension) will normally hold the MIME type.






      share|improve this answer























      • Unlike Avitus solution, this supports both .jpg and .jpeg

        – MSalters
        Mar 31 '10 at 12:59













      13












      13








      13







      HKCR\.<ext>Content Type (where "ext" is the file extension) will normally hold the MIME type.






      share|improve this answer













      HKCR\.<ext>Content Type (where "ext" is the file extension) will normally hold the MIME type.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 30 '10 at 23:07









      Jerry CoffinJerry Coffin

      387k50473915




      387k50473915












      • Unlike Avitus solution, this supports both .jpg and .jpeg

        – MSalters
        Mar 31 '10 at 12:59

















      • Unlike Avitus solution, this supports both .jpg and .jpeg

        – MSalters
        Mar 31 '10 at 12:59
















      Unlike Avitus solution, this supports both .jpg and .jpeg

      – MSalters
      Mar 31 '10 at 12:59





      Unlike Avitus solution, this supports both .jpg and .jpeg

      – MSalters
      Mar 31 '10 at 12:59













      0














      Pasted from http://www.snoyman.com/blog/2012/03/ie-mimetype-png.html:



      #include <urlmon.h>
      #include <stdio.h>

      int main(int argc, char* argv)

      char buff[256];
      LPWSTR out;

      FILE *in = fopen("title.png", "rb");

      fread(buff, 1, 256, in);

      FindMimeFromData(NULL, NULL, buff, 256, NULL, FMFD_DEFAULT, &out, 0);

      printf("%lsn", out);

      return 0;






      share|improve this answer



























        0














        Pasted from http://www.snoyman.com/blog/2012/03/ie-mimetype-png.html:



        #include <urlmon.h>
        #include <stdio.h>

        int main(int argc, char* argv)

        char buff[256];
        LPWSTR out;

        FILE *in = fopen("title.png", "rb");

        fread(buff, 1, 256, in);

        FindMimeFromData(NULL, NULL, buff, 256, NULL, FMFD_DEFAULT, &out, 0);

        printf("%lsn", out);

        return 0;






        share|improve this answer

























          0












          0








          0







          Pasted from http://www.snoyman.com/blog/2012/03/ie-mimetype-png.html:



          #include <urlmon.h>
          #include <stdio.h>

          int main(int argc, char* argv)

          char buff[256];
          LPWSTR out;

          FILE *in = fopen("title.png", "rb");

          fread(buff, 1, 256, in);

          FindMimeFromData(NULL, NULL, buff, 256, NULL, FMFD_DEFAULT, &out, 0);

          printf("%lsn", out);

          return 0;






          share|improve this answer













          Pasted from http://www.snoyman.com/blog/2012/03/ie-mimetype-png.html:



          #include <urlmon.h>
          #include <stdio.h>

          int main(int argc, char* argv)

          char buff[256];
          LPWSTR out;

          FILE *in = fopen("title.png", "rb");

          fread(buff, 1, 256, in);

          FindMimeFromData(NULL, NULL, buff, 256, NULL, FMFD_DEFAULT, &out, 0);

          printf("%lsn", out);

          return 0;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 '14 at 7:13









          Peter TsengPeter Tseng

          9,13314945




          9,13314945





















              0














              In Windows 10, the different MIME types are stored in the registry at:



              HKEY_CLASSES_ROOTMIMEDatabaseContent Type


              with a key for each content type (e. g. text/plain) under that key.






              share|improve this answer



























                0














                In Windows 10, the different MIME types are stored in the registry at:



                HKEY_CLASSES_ROOTMIMEDatabaseContent Type


                with a key for each content type (e. g. text/plain) under that key.






                share|improve this answer

























                  0












                  0








                  0







                  In Windows 10, the different MIME types are stored in the registry at:



                  HKEY_CLASSES_ROOTMIMEDatabaseContent Type


                  with a key for each content type (e. g. text/plain) under that key.






                  share|improve this answer













                  In Windows 10, the different MIME types are stored in the registry at:



                  HKEY_CLASSES_ROOTMIMEDatabaseContent Type


                  with a key for each content type (e. g. text/plain) under that key.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 18:20









                  Roger CookRoger Cook

                  464




                  464



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2549015%2ffinding-a-mime-type-for-a-file-on-windows%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線