1、gdal下载(自行下载,不在赘述)
2、配置系统环境变量
3、实现的关键代码
string strShpPath="shp文件地址";
string cmd_str="SET PGCLIENTENCODING=GBK && ogr2ogr -f \"PostgreSQL\" PG:\"host=localhost port=5432 dbname=postgres user=postgres password=postgres schemas=public\"" + " " + strShpPath+ " -lco GEOMETRY_NAME=geom -lco FID=pk_uid -lco encoding=GBK -nln wafang &exit";
call = mycall(cmd_str);
//调用cmd
private static bool mycall(string ogr2ogr_cmd_str)
{
try
{
var processStartInfo = new ProcessStartInfo()
{
Verb = "runas", // 如果程序是管理员权限,那么运行 cmd 也是管理员权限
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false, // 如果需要隐藏窗口,设置为 true 就不显示窗口
//StandardOutputEncoding = Encoding.UTF8,
Arguments = "/C " + ogr2ogr_cmd_str + " &exit",
};
Process.Start(processStartInfo);
}
catch (Exception e)
{
LogHelper.Error($"mycall方法捕获异常.参数信息:{ogr2ogr_cmd_str}." +
$"报错信息:{e.Message}.", null, "执行ogr2ogr调用异常");
return false;
}
return true;
}