c# - 从 C# 设置/运行 PostgreSQL

标签 c# .net postgresql localization process

我的要求如下:

  1. 应用程序启动后,PostgreSQL 服务将由应用程序启动
  2. 应用程序关闭后,应用程序将关闭 PostgreSQL 服务
  3. ...所以我将负责 PostgreSQL 设置、运行脚本和启动服务等

我目前的做法是:

  1. 当我在新进程中启动 PostgreSQL 时,我正在重定向 RedirectStandardErrorRedirectStandardOutput,这是一个静默启动,用户看不到命令​​窗口等

问题是,

  • 当我编写代码时,我寻找消息字符串并且只支持英语。换句话说,我以前在 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/

相关文章:

c# - 如果文件夹包含空格,如何处理文件路径中的空格?

c# - span<T> 和流

c# - 使用 C# 中的进度表将文件从 Web 下载到本地文件

c# - 我如何在 WPF 中读取 base64 图像?

c# - 在 .Net 中使用大于 2 个字节的 unicode 字符

regex - 在 Postgres 的 WHERE 中使用正则表达式

PostgreSQL 和 Glassfish EJB__TIMER__TBL 表

c# - 在 ASP.NET 中的其他 AppDomain 中运行应用程序

.net - 哪种模式/模式最适合开发规则/决策引擎

ruby-on-rails - 在 OpenSUSE 中安装 PG gem