我使用下面的代码C#Process实例为PostgreSQL进行CSV导入。
Process process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = @"cmd.exe",
Arguments = $"/c cat \"{filePath}\" | psql -h 127.0.0.1 -U {user} -d {dbname} -w -c \"copy data_temp from stdin csv header\" ",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Dispose();
它工作,但我查看任务管理器,一段时间后,有很多PostgreSQL进程在运行,最终数据库被锁定。
我没有正确关闭这个过程吗?我怎样才能确保它不会保持开放状态?
不确定这是否有帮助,但这里有两个通过msiexec和Exe查杀方法的例子。
卸载Msiexec.exe文件...
Process x = new Process();
x.StartInfo.FileName = "msiexec.exe";
x.StartInfo.Arguments = "/qb /x {81681F4C-83F1-4F22-9AEB-C7DA7C372EA2}";[quiet uninstall]
x.Start();
x.WaitForExit();
和
卸载exe(文件路径方法,而不是msiexec)
Process a = new Process();
a.StartInfo.FileName = JPRO_8_5_0; //defined in a string before hand.
a.StartInfo.Arguments = "/uninstall /quiet";
a.Start();
a.WaitForExit();
您可以尝试将文件名设置为之前定义的字符串,而不是将其作为@“cmd.exe”,这可能会有所帮助!