C# 中使用 Influxdb 1.x(四)——在程序内管理Influxdb服务的启停
在做单机软件开发的过程中,如果我们使用influxdb,且希望用户无感,需要完成在自己的程序中对influxdb服务进行开启和关闭管理
准备工站
influxdb服务软件放入项目中

属性更改
让软件内容复制到编译后后的目录中
全选文件,修改属性为:如果较新则复制

示例

启动
influxdb的启动,停止实现方式:
- 使用一个新的进程
Process - 进程启动参数
ProcessStartInfo,根据实际需要传递
// 创建进程启动信息ProcessStartInfo startInfo = new ProcessStartInfo();startInfo.FileName = fullExePath;startInfo.Arguments = $"-config \"{fullConfigPath}\""; // 命令参数startInfo.WorkingDirectory = influxdPath; // 设置工作目录为软件所在目录startInfo.UseShellExecute = false; // 不使用操作系统shell启动startInfo.RedirectStandardOutput = true; // 重定向输出startInfo.RedirectStandardError = true; // 重定向错误输出startInfo.CreateNoWindow = true; // 显示命令窗口
启动代码如下:
public static void StartInfluxd(string influxdPath){string executableName = "influxd.exe";string configFileName = "influxd.config";// 构建完整路径string fullExePath = Path.Combine(influxdPath, executableName);string fullConfigPath = Path.Combine(influxdPath, configFileName);// 检查文件是否存在if (!File.Exists(fullExePath)){throw new Exception($"错误: 未找到 {fullExePath}");}if (!File.Exists(fullConfigPath)){