mdb总是会出现乱码问题,读取中文图层类似于读取中文属性一样,写一个扩展方法,调用API来处理。首先得找到API的名称:
然后编写扩展方法:
[DllImport("gdal204.dll", EntryPoint = "OGR_L_GetName", CallingConvention = CallingConvention.Cdecl)] public static extern System.IntPtr OGR_L_GetName(HandleRef handle); /// <summary> /// 读取图层名称 /// </summary> /// <param name="layer">图层</param> /// <returns>图层名称</returns> public static string GetNameEx(this Layer layer) { IntPtr pName = OGR_L_GetName(OSGeo.OGR.Layer.getCPtr(layer)); return Marshal.PtrToStringAnsi(pName); }
最后轻松愉快地调用(实际并不轻松,GDAL乱码问题纠结了两天):
Layer layer = mdbDataSource.GetLayerByIndex(1); Console.WriteLine("图层:" + layer.GetNameEx());