从C ++扩展名获取MIME类型 [英] Get a MIME Type from a extension in C++
本文介绍了从C ++扩展名获取MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在给定文件扩展名的情况下,有什么方法可以获取C ++中的MIME类型吗?
Is there any way to can get the MIME Type in C++ given a file extension?
我已经阅读了有关HKEY_CLASSES_ROOT的信息,但老实说,我不知道如何使用它.
I have read about HKEY_CLASSES_ROOT, but I honestly have no idea of how to use it.
我想要的是输入内容
string extension=".pdf";
string extension2=".avi";
获得输出:
string mimeType ="application/pdf";
字符串mimeType2 ="video/x-msvideo";
string mimeType="application/pdf";
string mimeType2="video/x-msvideo";
我知道我可以自己做,但是我想这里已经完成了一些工作.
I know I could do this by myself, but I guess there is some work already done in here.
非常感谢
推荐答案
快到圣诞节了,怎么办?
As it's nearly Christmas, How about this:
#include <Windows.h>
#include <string>
using namespace std;
string GetMimeType(const string &szExtension)
{
// return mime type for extension
HKEY hKey = NULL;
string szResult = "application/unknown";
// open registry key
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szExtension.c_str(),
0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
// define buffer
char szBuffer[256] = {0};
DWORD dwBuffSize = sizeof(szBuffer);
// get content type
if (RegQueryValueEx(hKey, "Content Type", NULL, NULL,
(LPBYTE)szBuffer, &dwBuffSize) == ERROR_SUCCESS)
{
// success
szResult = szBuffer;
}
// close key
RegCloseKey(hKey);
}
// return result
return szResult;
}
int main(int argc, char* argv[])
{
string szExt1 = ".pdf";
string szExt2 = ".avi";
string szMime1 = GetMimeType(szExt1);
string szMime2 = GetMimeType(szExt2);
printf("%s = %s\n%s = %s\n", szExt1.c_str(), szMime1.c_str(),
szExt2.c_str(), szMime2.c_str());
return 0;
}
在我的系统上,它提供以下输出:
On my system, it gives the following output:
.pdf =应用程序/pdf
.avi =视频/avi
.pdf = application/pdf
.avi = video/avi
这篇关于从C ++扩展名获取MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文