Skip to content

Commit 21e195f

Browse files
committed
Retrieve Postgres system configuration directory
1 parent 3ebca76 commit 21e195f

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ protected override void GetProperties(Hashtable propertyDescriptors)
17471747

17481748
NpgsqlConnectionStringBuilder? GetConfigFromIniFile(string? filePath)
17491749
{
1750-
if (filePath is null || !File.Exists(filePath))
1750+
if (filePath is null || !File.Exists(filePath))
17511751
{
17521752
return null;
17531753
}

src/Npgsql/PostgresEnvironment.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Runtime.InteropServices;
45

@@ -81,5 +82,27 @@ internal static string? SslCertRootDefault
8182

8283
static string? GetSystemPostgresDir() => Environment.GetEnvironmentVariable("PGSYSCONFDIR");
8384

84-
static string? GetDefaultSystemPostgresDir() => default; // Retrieve from "pg_config --sysconfdir"
85+
static string? GetDefaultSystemPostgresDir() => ExecuteCommandSync("pg_config", "--sysconfdir");
86+
87+
static string? ExecuteCommandSync(string command, string arguments)
88+
{
89+
try
90+
{
91+
using var process = new Process();
92+
process.StartInfo.FileName = command;
93+
process.StartInfo.Arguments = arguments;
94+
process.StartInfo.RedirectStandardOutput = true;
95+
process.StartInfo.UseShellExecute = false;
96+
process.Start();
97+
98+
var output = process.StandardOutput.ReadToEnd().Trim();
99+
process.WaitForExit();
100+
101+
return process.ExitCode == 0 ? output : null;
102+
}
103+
catch (Exception)
104+
{
105+
return null;
106+
}
107+
}
85108
}

0 commit comments

Comments
 (0)