© 2011 autodesk configure your.net application – the other ini jason huffine electrical engineer,...

37
© 2011 Autodesk Configure Your .Net Application – The Other INI Jason Huffine Electrical Engineer, Substation Projects, Tennessee Valley Authority

Upload: fay-douglas

Post on 12-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

© 2011 Autodesk

Configure Your .Net Application – The Other INI

Jason HuffineElectrical Engineer, Substation Projects, Tennessee Valley Authority

© 2011 Autodesk

Class Summary

This class is intended to introduce .Net configuration files to those developing .Net applications for AutoCAD.

We will take a look at the background and basic concepts of the .Net configuration file class architecture.

We will cover creation of, modification of, and reading of basic, external, and customized configuration files.

Finally, we’ll look at some fairly practical examples for a more real-world feel of how this technology can be applied

© 2011 Autodesk

Learning Objectives

At the end of this class, you will be able to: Understand the basic mechanics and fundamental elements of the Microsoft

configuration system. Understand how to create/modify your application’s configuration file. Understand how to use the .Net configuration classes to interface with the

configuration file. Understand how to use the .Net framework and its configuration classes to create

and handle custom configuration elements. Understand how to persist custom object data in a configuration file.

© 2011 Autodesk

Assumptions/Givens

Here are a few assumptions/givens for the class: This class will focus on the application (exe) context Familiarity with setting up a VS .Net Project for AutoCAD Example code written around 4.0 Framework, but should work with 3.5 as well Example code written using C# syntax Free online code language translators:

http://www.developerfusion.com/tools/ http://converter.telerik.com/

Familiarity with reading and writing plain-text and xml files

Be able to “duck” or catch when objects are airborne!

© 2011 Autodesk

Configuration File Background

© 2011 Autodesk

Why Use Configuration Files?

In lieu of an INI File INI Files are only as standard as the developer makes it!

When a small data store is needed When applications need to share configuration data In lieu of hard-coding settings or program constants When strongly-typed data needs to be read, modified, and saved When needing predefined .Net Configuration Elements:

<ConnectionStrings> <dependentAssembly> <cryptographySettings>

© 2011 Autodesk

Background… The Road from the INI days

Application INItialization files have been around a long, long time Plain-text files with section tags and special characters Format inconsistencies:

Yes or no to whitespace? Quoted values use ‘ or “ ? Comments indicated with # or ; ? Property/value delimiter be : or = ?

Parsing code must be custom-developed P/Invoke Custom text-file parsing methods

Typically no file hierarchy without much customization

© 2011 Autodesk

So if I ask you….

Which Windows operating systems introduced Initialization File Mapping to aid developer migration of data stored in classic INI files to the new (at that time) Windows Registry?

Answer: Windows NT and Windows 95

Source: Someone smarter (and older) than me on Wikipedia®

© 2011 Autodesk

Configuration File Fundamentals

© 2011 Autodesk

Types of Configuration Files…

Machine Configuration File: Machine.config Stores configuration data for ALL applications running within the CLR 2.0 & 3.5 Framework => C:\Windows\Microsoft.NET\Framework\v2.0.50727\Config 4.0 Framework => C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config

Application Configuration File: [AppName].exe.config or [AppName].dll.config Stores any application-level data Defines default values for Roaming-User and Local-User configuration files

© 2011 Autodesk

Types of Configuration Files…

Roaming-User Configuration File: User.config A higher-level, user configuration file Used to store user-specific settings data Also typically used with Windows roaming profiles Location => %UserProfile

%\Application Data\[Company Name]\[Application Name]_[Evidence Type]_[Evidence Hash]\[Version]\user.config

Local-User Configuration File: User.config The most specific configuration file Typically used to store user-specific settings data Location => %UserProfile

%\Local Settings\Application Data\[Company Name]\[Application Name]_ [Evidence Type]_[Evidence Hash]\[Version]\user.config

© 2011 Autodesk

Types of Configuration Files…

Separate, External Configuration Files: [ConfigName].config Auxiliary files that can be used to separate settings data Can be merged with application-level configuration files Use the predefined <appSettings> element

© 2011 Autodesk

The Merging Concept

Application exe Configuration:

acad.exe.configacad.exe.config [Requires Custom Merging]

Machine Configuration: Machine.configMachine Configuration: Machine.config

[MyCADApp].dll.config[MyCADApp].dll.config

Roaming-User Configuration: User.config

Roaming-User Configuration: User.config

Local-User Configuration:

User.config

Local-User Configuration:

User.config

External Configuration: [AppSettings].config

External Configuration: [AppSettings].config

ME

RG

ING

ME

RG

ING

© 2011 Autodesk

Configuration Elements, Sections, and Groups<!--Configuration Group--><example.group> <!--Section with attributes--> <example.section dateModified="7/7/2011">

<!--Basic Element Nested in Section--> <BasicElement dateTimeValue="7/7/2011" intValue="100" />

<!--Element Collections--> <myElementCollection> <add type="cake" flavor="carrot"/> <add type="pie" flavor="coconut"/> <add type="cheesecake" flavor="chocolate"/> </myElementCollection>

</example.section> <!--Now add as many Sections as necessary-->

</example.group>

© 2011 Autodesk

So if I ask you….

Which direction does the .Net CLR merge configuration files?

Answer: Most-specific to least-specific…. or Local-User → Roaming-User → Application → Machine

© 2011 Autodesk

Basic Configuration Files

© 2011 Autodesk

Creating a Basic Configuration Files

Create using Settings tab of the Project Designer:

app.config template configuration file is automatically created! DO NOT change the name of app.config!

Application-scoped settings: Administrator use, cannot be updated during runtime and is saved in [MyCADApp].dll.config

User-scoped settings: Local-User, User.config settings that can be updated during runtime

© 2011 Autodesk

Interacting with Basic Configuration Files

Reading settings data:

Modifying settings data (user-scoped only!):

Obtaining original, default settings values:

'VBDim acadColor As Autodesk.AutoCAD.Colors.Color = My.Settings.myColor

//C#Autodesk.AutoCAD.Colors.Color acadColor = MyCADApp.Properties.Settings.Default.myColor;

'VBMy.Settings.myColor = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 0)My.Settings.Save()

//C#MyCADApp.Properties.Settings.Default.myColor = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 0);MyCADApp.Properties.Settings.Default.Save();

'VBDim strMyColor As String = My.Settings.Properties("myColor").DefaultValue

//C#string strMyColor = (string) MyCADApp.Properties.Settings.Default.Properties["myColor"].DefaultValue;

© 2011 Autodesk

Create and Modify Basic Configuration Files…

© 2011 Autodesk

So if I ask you….

Which configuration scope assignment from the Settings tab in the VS Project Designer allows runtime modifications?

Answer: User

© 2011 Autodesk

External Configuration Files

© 2011 Autodesk

Creating an External Configuration File

Add the file to the VS Solution: Right-click over project title → Add/New Item… → select ‘Application Configuration File’ It’s okay to rename this one!

Add elements between <configuration> element tags: The <appSettings> element will contain the settings Add as many <add key=“” value=“”> key/value pair collection elements as needed Data values are string values… not strongly-typed

<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="fontFilePath" value="C:/Program Files/AutoCAD 2012/Fonts"/> <add key="bmpFilePath" value="C:/Program Files/AutoCAD 2012/Icons"/> <add key="plotstylesFilePath" value="C:/Program Files/AutoCAD 2012/Plot Styles"/> <add key="myInteger" value="1050"/> <add key="myColor" value="Red"/> <add key="myLine" value="Line{Point1:(0,0,0),Point2:(5,5,5)}"/> </appSettings></configuration>

© 2011 Autodesk

Encapsulating an External Configuration File

By default, the CLR assumes the configuration file is located in the same directory as the executable… therefore, an external configuration file requires encapsulation:

Configuration class represents a merged view of configuration settings

ConfigurationManager class provides access to the configuration files OpenMappedExeConfiguration() method returns a Configuration class representation of a directory-

specified configuration file ExeConfigurationFileMap class parameter maps the Exe, Local-User, Machine, and Roaming-User configuration

files Ideal for external, auxiliary configuration files!

OpenExeConfiguration() method returns a Configuration class representation of host application Great for accessing acad.exe.config! Also great for accessing customized configuration files!

© 2011 Autodesk

Encapsulating an External Configuration File

OpenMappedExeConfiguration() method

OpenExeConfiguration() method

ConfigurationUserLevel: None – Gets the application configuration PerUserRoaming – Gets the Roaming-User (user.config) configuration PerUserRoamingAndLocal – Gets the Local-User (user.config) configuration

//Get the acad.exe configuration fileConfiguration acadConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//Get the auxiliary configuration fileExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

fileMap.ExeConfigFilename = @”C:\...\Auxiliary.config";

Configuration auxConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

© 2011 Autodesk

Reading an External Configuration File

Read an external configuration file

//The iterative approachforeach (string key in auxConfig.AppSettings.Settings.AllKeys){

if (auxConfig.AppSettings.Settings[key] != null) { string value = auxConfig.AppSettings.Settings[key].Value; }}

//The basic and straight forward approachstring key = auxConfig.AppSettings.Settings["myColor"].Key;string value = auxConfig.AppSettings.Settings["myColor"].Value;

//Shorten retrieval of data by creating references to the appSettings sectionAppSettingsSection appSettings = config.AppSettings;value = appSettings.Settings["myColor"].Value;

© 2011 Autodesk

Modifying an External Configuration File

//Add a new settingauxConfig.AppSettings.Settings.Add("myKey", "myValue");

//Remove another settingauxConfig.AppSettings.Settings.Remove("anotherKey");

//Modify a settingauxConfig.AppSettings.Settings[“myKey"].Value = "somevalue";

//Save and refresh when finished...auxConfig.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("appSettings");

© 2011 Autodesk

Create and Modify External Configuration Files…

© 2011 Autodesk

So if I ask you….

Which class in the System.Configuration namespace encapsulates a merged view of the configuration settings?

Answer: The Configuration class

© 2011 Autodesk

Custom Configuration Files

© 2011 Autodesk

Configuration Element Handling…

Every custom element, section, and group has to be represented in code! Representation is easy… create a class with properties to represent element

attributes or section/group nested elements Each class has to inherit from the appropriate System.Configuration namespace reference:

Elements: ConfigurationElement class Element Collection: ConfigurationElementCollection class Section: ConfigurationSection class Group: ConfigurationSectionGroup class

Each class can be developed using one of two approaches: Programmatic Declarative

© 2011 Autodesk

Create and Modify Custom Configuration Files…

© 2011 Autodesk

So if I ask you….

Customized configuration elements, sections, and groups require the developer to create handling code for each. The properties that represent element tag attributes can be written which two ways?

Answer: Programmatic and Declarative

© 2011 Autodesk

Demonstrations

© 2011 Autodesk

Basic AutoCAD Setup…

© 2011 Autodesk

Custom Block Configuration…

© 2011 Autodesk

So if I ask you….

Fill in the blank:The objects that ensure data correctness of element attribute values entered in the

configuration file are called ______________________ while the objects that allow custom data to be saved to a configuration file when default serialization/ deserialization processes will not work are called _________________.

Answer: Validators, Converters

© 2011 Autodesk

Autodesk, AutoCAD* [*if/when mentioned in the pertinent material, followed by an alphabetical list of all other trademarks mentioned in the material] are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document. © 2011 Autodesk, Inc. All rights reserved.