2013-02-19 17 views
0

我想一個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

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