Author Topic: Using app.config  (Read 3153 times)

0 Members and 1 Guest are viewing this topic.

TimBlanch

  • Guest
Using app.config
« on: November 04, 2011, 08:55:47 AM »
I wanted to use a key - value pair list for inserting blocks. When I insert a block it would look at the list and find the layer it belongs on. So I decided to use the app.config file thinking that would be the easiest, WRONG. My app config looks like this:
Code: [Select]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="block1" value="testlayer1"/>
    <add key="block2" value="testlayer2"/>
  </appSettings>
</configuration>
and my code looks like this:
Code: [Select]
string blockName = "block1";
AppSettingsReader appReader = new AppSettingsReader();
string layer = appReader.GetValue(blockName, typeof(string)).ToString();

I get an error that says " the key "block1" does not exist in the appSettings configuration section. I tried a standard console project with a copy of the app.config and it returns the layer name with no problem.
Has anyone tried using the app.config with their project yet?

I am using:
.Net 4
Acad 2012
WinXP 64

Thanks,
Tim

n.yuan

  • Bull Frog
  • Posts: 348
Re: Using app.config
« Reply #1 on: November 04, 2011, 12:03:59 PM »
Because your app is a AutoCAD add-in (a DLL), which is hosed by the real app AutoCAD (strictly speaking, your "app" is not an app, is is part of hosting app AutoCAD.

So, the real app AutoCAD only knows acad.exe.config as its *.config file. Thus, you need add the <appSettings /> section to acad.exe.config.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Using app.config
« Reply #2 on: November 04, 2011, 12:14:53 PM »
Addition to my previous reply:

<appSettings /> in acad.exe.config is for application level settings. If you want to store per user settings, you can still use VS built-in mechanism: open project properties window and add settings iin "Settings" tab". Make sure the settings' scope is set to "User".

Then you can access the settings in the code:

string mysetting1=Properties.Settings.Default.MySetting1;

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Using app.config
« Reply #3 on: November 04, 2011, 02:51:32 PM »
Addition to my previous reply:

<appSettings /> in acad.exe.config is for application level settings. If you want to store per user settings, you can still use VS built-in mechanism: open project properties window and add settings iin "Settings" tab". Make sure the settings' scope is set to "User".

Then you can access the settings in the code:

string mysetting1=Properties.Settings.Default.MySetting1;

These are saved in some user.config file somewhere deep inside Documents and Settings.

They work per AutoCAD version so if you change from 2011 to 2012 you have to set all the settings again. And if you use both versions they both can have different settings. Also, one mistake with your Documents and Settings system, all settings are reset. And what if you work in an environment where people will get a fresh desktop every day?

I can advice to store settings in the Registry or an external file of which you are sure you have full access.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Using app.config
« Reply #4 on: November 04, 2011, 05:09:36 PM »
I can advice to store settings in the Registry or an external file of which you are sure you have full access.

+1.  Its also less destructive (your application, rather than the entirety of that ACAD.EXE) if somebody borks the file while making a minor change, like adding a block.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}