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 保存文件。

2)以替代方式导入GetPrivateProfileSectionNames以满足您的要求。

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

3) 将调用更改PtrToStringAnsiPtrToStringUni

这些步骤实现了你想要的,整个代码是

[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 回答