tags:

views:

817

answers:

4
+1  Q: 

Config file in c#

I have a UserControl Library solution which has the following

UserControl ---UserControl project

  ---UserControl Test Project

IN my test project, I am able to add my usercontrol to the tool box. When i drag it and drop it in my forms, it fails. I put in logging and found out that my usercontrol reads a config file. The config file is marked to copy always and exists in the obj\debug and the bin\debug directory/.

However when i drag the usercontrol to a form on my test project, it is trying to get a file from

C:\Documents and Settings\jondoe\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies\vqjlihdl01

The above is a result of this

string pluginAssemblyPath = Assembly.GetExecutingAssembly().Location;
            DirectoryInfo dirInfo = new DirectoryInfo(pluginAssemblyPath);
            pluginAssemblyPath = pluginAssemblyPath.Replace(dirInfo.Name.ToString(),"");
            string configFilePath = pluginAssemblyPath + "FileConfig.xml";

I would have assumed that if i compile in debug mode, the file should be under obj\debug and that should be my assembly path. what gives or is there some setting that i need to do to get it to run correctly so that it can find my config file in the right location?

A: 

Do you need the config file at design time? If not, you could change your code to test if it is running at design time, something like:

if (this.Site != null && this.Site.DesignMode)
{
    ... design time behavior
}
else
{
    ... runtime behavior (read config file)
}
Joe
A: 

Does your user control really need to read a config file? One of the primary advantages of making a user control a separate project is that you can reuse it in multiple other projects. If your user control has to read a config file, that makes reuse of it more difficult and complicated (as you've seen).

If you don't actually need to use the control in multiple projects, the quickest solution to your current problem might be to just incorporate the user control into your main project (i.e. don't keep it as a separate project).

MusiGenesis
A: 

I have to use my control in various projects and hence i used the config file to tweak settings based on the project.

I didn't see this issue with other projects where i have done work in the similar manner. it always used the obj\debug path to build the assembly execution path

Normally you would put this in the comments under my answer (I assume you were responding to me). The answers are for, well, answers. :-)
MusiGenesis
+1  A: 

Beware, I have found the DesignMode property to be unreliable where you have a control on another control on a form (say). It only seems to work for controls placed directly on the design surface.

kpollock
+1 Interesting. I'll have to try that out.
MusiGenesis
well, certainly let me know if you get it to work!
kpollock