[英]How can I build a DataTable schema from schema.ini with special characters
I'm trying to convert a schema.ini file (created by a csv export) to a typed data table in c#. 我正在尝试将schema.ini文件(由csv导出创建)转换为c#中的类型化数据表。 I'm having troubles with getting the table names from the file.
我在从文件中获取表名时遇到麻烦。 The schema.ini looks like this:
schema.ini如下所示:
[MyTableÄ.csv]
ColNameHeader=True
CharacterSet=1201
Format=CSVDelimited
CurrencyThousandSymbol=,
DecimalSymbol=.
Col1="Id" Integer
Col2="Subscriber Equipment" Char ...
however when I use the following snippet to read the section names I get MyTableÄ.csv
instead of MyTableÄ.csv
. 但是,当我使用以下代码片段读取节名称时,我得到的是
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');
}
I've tried the charactersets: 20127, ANSI, 65001, Unicode, and 1201
to no avail. 我尝试了以下字符集:
20127, ANSI, 65001, Unicode, and 1201
无济于事。 Ideas? 有想法吗? Workarounds?
解决方法?
1) Save your file with Unicode. 1)使用Unicode保存文件。
2) Import GetPrivateProfileSectionNames
in an alternative way to meet your requirement. 2)以其他方式导入
GetPrivateProfileSectionNames
以满足您的要求。
[DllImport("kernel32.dll", EntryPoint="GetPrivateProfileSectionNamesW", CharSet=CharSet.Unicode)]
static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);
3) Change the call of PtrToStringAnsi
to PtrToStringUni
. 3)将
PtrToStringAnsi
的调用更改为PtrToStringUni
。
These steps achieve what you want, the entire code is 这些步骤实现了您想要的,整个代码是
[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, your code appears [ here ] on MSDN forum. 顺便说一句,您的代码出现在[ 此处 ]在MSDN论坛上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.