0

schema.iniファイル(csvエクスポートによって作成された)をc#の型付きデータテーブルに変換しようとしています。ファイルからテーブル名を取得するのに問題があります。schema.iniは次のようになります。

[MyTableÄ.csv]
ColNameHeader=True
CharacterSet=1201
Format=CSVDelimited
CurrencyThousandSymbol=,
DecimalSymbol=.
Col1="Id" Integer
Col2="Subscriber Equipment" Char ...

ただし、次のスニペットを使用して、のMyTableÄ.csv代わりに取得したセクション名を読み取るとMyTableÄ.csv

public string[] SectionNames()
        {
        uint MAX_BUFFER = 32767;
        IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
        uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
        if (bytesReturned == 0)
        {
            Marshal.FreeCoTaskMem(pReturnedString);
            return null;
        }
        string local = Marshal.PtrToStringAnsi(pReturnedString, (int) bytesReturned);
        Marshal.FreeCoTaskMem(pReturnedString);
        //use of Substring below removes terminating null for split
        return local.Substring(0, local.Length - 1).Split('\0');
    }

私はキャラクターセットを試しました:20127, ANSI, 65001, Unicode, and 1201無駄に。アイデア?回避策?

4

1 に答える 1

1

1)ファイルをUnicodeで保存します。

GetPrivateProfileSectionNames2)要件を満たすために別の方法でインポートします。

[DllImport("kernel32.dll", EntryPoint="GetPrivateProfileSectionNamesW", CharSet=CharSet.Unicode)]
static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);

3)の呼び出しをPtrToStringAnsiに変更しますPtrToStringUni

これらのステップはあなたが望むものを達成します、コード全体は

[DllImport("kernel32.dll", EntryPoint="GetPrivateProfileSectionNamesW", CharSet=CharSet.Unicode)]
static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);

public string[] SectionNames() {
    uint MAX_BUFFER=32767;
    IntPtr pReturnedString=Marshal.AllocCoTaskMem((int)MAX_BUFFER);
    uint bytesReturned=GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
    if(bytesReturned==0) {
        Marshal.FreeCoTaskMem(pReturnedString);
        return null;
    }
    string local=Marshal.PtrToStringUni(pReturnedString, (int)bytesReturned);
    Marshal.FreeCoTaskMem(pReturnedString);
    //use of Substring below removes terminating null for split
    return local.Substring(0, local.Length-1).Split('\0');
}

ところで、あなたのコードはMSDNフォーラムに[ここ]に表示されます。

于 2013-02-19T19:07:57.580 に答える