exePath is invalid exception in ConfigurationManager.OpenExeConfiguration
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 m开发者_高级运维y 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
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.
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 :)
Give this a try, it has helped me in the past: http://sandblogaspnet.blogspot.com/2009/06/reading-from-configuration-file-in-net.html
精彩评论