如何建立从SCHEMA.INI一个DataTable的架构有特殊字符

问题描述:

我想一个schema.ini文件(通过CSV导出创建)转换到类型化的数据表在C#中。我在从文件中获取表名时遇到麻烦。该SCHEMA.INI看起来是这样的:如何建立从SCHEMA.INI一个DataTable的架构有特殊字符

[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无济于事。想法?解决方法?

1)除使用Unicode文件。

2)进口GetPrivateProfileSectionNames用另一种方式来满足您的要求。

[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'); 
} 

BTW,会出现代码[here]在MSDN论坛。

+0

谢谢!我认为代码是从其他地方复制过来的,但我不知道源代码。我已经更新了我们如何将schema.ini编写为Unicode编码,并进行了您所建议的更改以及一切正常。 – Kim 2013-02-19 20:06:27