我的要求如下:
- 应用程序启动后,PostgreSQL 服务将由应用程序启动
- 应用程序关闭后,应用程序将关闭 PostgreSQL 服务
- ...所以我将负责 PostgreSQL 设置、运行脚本和启动服务等
我目前的做法是:
- 当我在新进程中启动 PostgreSQL 时,我正在重定向
RedirectStandardError
和RedirectStandardOutput
,这是一个静默启动,用户看不到命令窗口等
问题是,
- 当我编写代码时,我寻找消息字符串并且只支持英语。换句话说,我以前在
RedirectStandardOutput
中查找字符串success
但现在我们支持多种语言 所以比较失败。
有什么方法可以知道 PostgreSQL 设置是否成功启动以及 PostgreSQL 服务是否正在运行?
我通过在单独的进程中调用 pg_ctl.exe 来启动 PostgreSQL。
最佳答案
(我们正在做类似的事情)
您使用这个批处理文件启动 Postgres
服务
"C:\Program Files\PostgreSQL\9.0\bin\pg_ctl.exe" -D "C:\Program Files\PostgreSQL\9.0\data" start
和[停止]服务
"C:\Program Files\PostgreSQL\9.0\bin\pg_ctl.exe" -D "C:\Program Files\PostgreSQL\9.0\data" stop
C:\Program Files\PostgreSQL\9.0\bin\pg_ctl.exe
是您安装PostgreSQl
的位置,您可以从中获取
HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0
现在运行批处理文件来检查服务是否确实在运行,您可以使用
此代码来自 Process running
检查 postgres.exe
是否正在运行。
还有 1. checking-if-windows-application-is-running 2. how-can-i-know-if-a-process-is-running
public bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses()) {
//now we're going to see if any of the running processes
//match the currently running processes. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.
//Remember, if you have the process running more than once,
//say IE open 4 times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
if (clsProcess.ProcessName.Contains(name))
{
//if the process is found to be running then we
//return a true
return true;
}
}
//otherwise we return a false
return false;
}
关于c# - 从 C# 设置/运行 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908557/