3

ファイル名が与えられ、ディスクからそのファイル名を読み取って、その内容をネットワーク経由で送信できる必要があります。ファイルがテキストかバイナリかを判断できる必要があるので、StreamReaderとBinaryReaderのどちらを使用するかがわかります。コンテンツタイプを知る必要があるもう1つの理由は、コンテンツタイプがバイナリの場合、データをネットワーク経由で送信する前にデータをMIMEエンコードする必要があるためです。また、コンテンツタイプが何であるか(テキストの場合はエンコーディングを含む)を消費者に伝えたいと思います。

ありがとう!

4

3 に答える 3

2

ファイル名の拡張子は、ファイルのコンテンツ タイプに関する最良のヒントを提供します。

完璧ではありませんが、以下を使用していくつかの成功を収めました。

private static string GetContentTypeFromRegistry(string file)
{
    RegistryKey contentTypeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type");

    foreach (string keyName in contentTypeKey.GetSubKeyNames())
    {
        if (System.IO.Path.GetExtension(file).ToLower().CompareTo((string)contentTypeKey.OpenSubKey(keyName).GetValue("Extension")) == 0)
        {
            return keyName;
        }
    }

    return "unknown";
}

private static string GetFileExtensionFromRegistry(string contentType)
{
    RegistryKey contentTypeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + contentType);

    if (contentTypeKey != null)
    {
        string extension = (string)contentTypeKey.GetValue("Extension");

        if (extension != null)
        {
            return extension;
        }
    }

    return String.Empty;
}
于 2009-02-11T17:08:53.260 に答える
1

事前にファイルの種類を知るか、ファイルの種類を知らされる必要があります。また、ネットワーク経由でファイルを送信するだけの場合は、Stream.Write メソッドと Stream.Read メソッドを使用してみませんか? サービスの利用者がファイルの種類を決定できるようにします。サーバー上のデータを解釈しないため、*Reader クラスを使用する必要はありません。

于 2009-02-11T17:08:44.320 に答える
0

この無料の MIME 検出ライブラリを試すことができます。 http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx

于 2009-02-11T17:34:33.917 に答える