Problems with Application.ExecutablePath

A

Alberto

In a class called "Config.cs" I have declared a field like this:

private string path = Path.GetDirectoryName(Application.ExecutablePath);

This class is initialized in the constructor of a UserControl (Config config
= new Config()).

When I execute the application everything is fine but if I try to add the
UserControl to a form, an error message says "access deny to "C:\Program
files\Microsoft Visual Studio 9.0\Common7\IDE\MyConfig.xml". So, as you see,
when I run the app, Application.ExecutablePath returns the right executable
path but when I'm building the forms in design time, returns a wrong
directoty.
 
P

Peter Duniho

Alberto said:
In a class called "Config.cs" I have declared a field like this:

private string path = Path.GetDirectoryName(Application.ExecutablePath);

This class is initialized in the constructor of a UserControl (Config
config = new Config()).

When I execute the application everything is fine but if I try to add
the UserControl to a form, an error message says "access deny to
"C:\Program files\Microsoft Visual Studio 9.0\Common7\IDE\MyConfig.xml".
So, as you see, when I run the app, Application.ExecutablePath returns
the right executable path but when I'm building the forms in design
time, returns a wrong directoty.

I haven't looked closely enough at the scenario to tell you for sure why
the error happens. However, it seems pretty self-explanatory.

I'd say more problematic is your use of a class that is apparently
somehow tied to the application configuration, as a general-purpose
control. Your control should not have this sort of dependency on the
application and while there may be a work-around (for example, detect
the design scenario and avoid accessing the denied resource then), IMHO
you will be better off taking the hint from the tools and changing your
code design so that the UserControl is genuinely independent of the
application-specific configuration.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top