I'm developing a WinForm application with C# and .NET Framework 4.7.
With this application I'm trying to load the configuration file from another application:
string applicationName = Environment.GetCommandLineArgs()[1];
if (!string.IsNullOrWhiteSpace(applicationName))
{
if (!applicationName.EndsWith(".exe"))
applicationName += ".exe";
string exePath =
Path.Combine(Environment.CurrentDirectory, applicationName);
try
{
// Get the configuration file. The file name has
// this format appname.exe.config.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(exePath);
But ConfigurationManager.OpenExeConfiguration(exePath)
throw an exception:
An error occurred while loading the configuration file: The 'exePath' parameter is not valid.
Parameter name: exePath
The configuration file, AnotherApp.exe.config, exists in the folder Environment.CurrentDirectory
. I have also tried changing it to Path.Combine(@"D:\", applicationName);
and I get the same exception.
If I add the exe.config
, instead of .exe
, at the end of the name here, applicationName += ".exe";
, it seems to open something: config.FilePath
is D:\AnotherApp.exe.config.config
. But config
object is empty. It hasn't filled any property.
What am I doing wrong?
I have copied the code from Microsoft documentation.
Before attempting to open AnotherApp.exe.config
, ConfigurationManager.OpenExeConfiguration
checks to see whether AnotherApp.exe
exists on disk. Here's the source:
// ...
else {
applicationUri = Path.GetFullPath(exePath);
if (!FileUtil.FileExists(applicationUri, false))
throw ExceptionUtil.ParameterInvalid("exePath");
applicationFilename = applicationUri;
}
// Fallback if we haven't set the app config file path yet.
if (_applicationConfigUri == null) {
_applicationConfigUri = applicationUri + ConfigExtension;
}
As you can see, the exePath
is ultimately passed into FileUtils.FileExists
, which ends up checking whether exePath
represents a file on disk. In your case, this is AnotherApp.exe
, which does not exist. The throw ExceptionUtil.ParameterInvalid("exePath");
statement is where your error is coming from.
Further down in the source I've included above, you can see _applicationConfigUri
is set to AnotherApp.exe.config
(it's an absolute path, but I've used a relative path for simplicity). When you set your exePath
to AnotherApp.exe.config
, the code ends up checking for the existence of AnotherApp.exe.config
(it thinks this is the exe itself), which it finds. After this, _applicationConfigUri
is set to AnotherApp.exe.config.config
which does not exist, but the configuration system does not error out in this scenario (instead returning an empty configuration object).
It appears there may be two options for solving this:
AnotherApp.exe
alongside AnotherApp.exe.config
.ConfigurationManager.OpenMappedExeConfiguration
, which allows you to provide your own ExeConfigurationFileMap
for instructing the configuration system on how to locate the .config
file. If you need help with this, let me know and I'll include an example of how this should work.