Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exePath is invalid exception in ConfigurationManager.OpenExeConfiguration

Tags:

c#

I have a windows service which is installed to C:\Windows\System32. It has a usual .config file, where some app settings are stored. I have another app that writes some values to this config. When i run this app it throws an exception at this line

var config = ConfigurationManager.OpenExeConfiguration(serviceExePath);

The exception says: An error occurred loading a configuration file: The parameter 'exePath' is invalid. Parameter name: exePath

When I put my windows service to another folder everything is ok! Is it due to some access violation rules or smth like that? Is there any way to use System32 folder for my service and open its config?

OS: Windows 7 x64

like image 552
Daniel Vygolov Avatar asked Jul 19 '10 16:07

Daniel Vygolov


2 Answers

It is a poor choice for a file location. That directory belongs to Windows, it isn't suitable for your own apps. For one, you'll need admin privileges to open files in that directory. You don't get that without a manifest to trigger the UAC prompt.

For another, that directory is virtualized on the x64 version of Windows. A 32-bit app trying to access files there will be redirected to c:\windows\syswow64.

I could have been more accurate if you posted the stack trace. But, just don't do it.

like image 150
Hans Passant Avatar answered Oct 03 '22 21:10

Hans Passant


Note that despite the name of the parameter being 'exePath', you should be passing in the name of the .config file, so you might need to append '.config' based on what's in your serviceExePath folder.

http://msdn.microsoft.com/en-us/library/ms224437.aspx

exePath

Type: System.String

The path of the configuration file. The configuration file resides in the same directory as the executable file.

As it stands, I get the feeling it's trying to load the actual .exe as a config file, which certainly explains why it considers it to be invalid :)

like image 26
James Manning Avatar answered Oct 03 '22 22:10

James Manning