Custom ConfigurationManager of Wince

Source: Internet
Author: User

Recently, the WINCE program is about to be tested. many items on the client must be moved to the configuration file, so we thought about inertial thinking.System. Configuration. ConfigurationManager class.As always, we add an XML file and rename itAPP. CONFIG,Modify the content

<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>


<Deleetask>

<! -- DeviceSvc address -->
<Add key = "DeviceSvcAddress" value = "http: // ************/DeviceSvc. svc"/>
</AppSettings>

</Configuration>

After saving it, it is easy to call it locally.

  var filePath = ConfigurationManager.AppSettings["DeviceSvcAddress"];

After that, I was dumpfounded and found that CompactFramework, a streamlined. NET Framework, does not support ConfigurationManager. What should I do? This configuration string cannot be placed in the code. Now, ConfigurationManager is nothing more than an XML file reading class. CompactFramework fully supports XML reading and writing, so we cannot write one by ourselves.

Before you start, you can sort out the requirements. What does this class do:

1. It is best to use static files for convenient calls.

2. Read the APP. Config.

3. It would be better if you could add or modify it again.

 

OK. Determine the requirement.

Public static class AppConfiger
{
Public static string _ configName = string. Empty;
Private static XDocument _ configDoc = null;
Private static string _ path = string. Empty;

Static AppConfiger ()
{
// Compile the CAB file and change it
// _ ConfigName = "Cp.Device.exe. Config ";

// Used for normal debugging
// # If DEBUG
// _ ConfigName = "App. Config ";
// # Endif
// # If! DEBUG
// _ ConfigName = "Cp.Device.exe. Config ";
// # Endif

LoadConfigurationFile (_ configName );
}

Private static void LoadConfigurationFile (string configName)
{
_ ConfigDoc = new XDocument ();
String appDir = Path. GetDirectoryName (Assembly. GetExecutingAssembly (). GetName (). CodeBase );
_ Path = Path. Combine (appDir, configName );
_ ConfigDoc = XDocument. Load (_ path );

}


}

This is an attribute and constructor of this class. _ configName is the name of the configuration file.

// Compile the CAB file and change it
// _ ConfigName = "Cp.Device.exe. Config ";

// Used for normal debugging
// # If DEBUG
// _ ConfigName = "App. Config ";
// # Endif
// # If! DEBUG
// _ ConfigName = "Cp.Device.exe. Config ";
// # Endif

This section makes me very entangled. There are two running methods for the WINCE program Deploye,

One is a direct connection development machine. after VS is run with F5, VS can be released once. You can modify the RELEASE path of the program. In this way, the DEBUG and RELEASE modes are also used. For some reason I don't know, the file published in this way does not exist *. exe. the configuration file like config is generated, so I set the APP. change the CONFING attribute to Contant + copy always. Then read the configuration using the app. config file.
Another method is to compile the CAB package for installation, which is to simulate the normal release process. In this way, * is generated *. exe. in the config file, I thought there would also be a distinction between DEBUG and RELEASE in this mode, so I used the "# if DEBUG" Compiling path to distinguish, the result shows that no matter which method the compiled CAB package will go to _ configName = "App. config "; this branch is too unprofessional if there are two configuration files when the program is released.

So I changed my mind to read the configuration file.

Private static void LoadConfigurationFile ()
{

Var configName = "App. Config ";
_ ConfigDoc = new XDocument ();
String appDir = Path. GetDirectoryName (Assembly. GetExecutingAssembly (). GetName (). CodeBase );
_ Path = Path. Combine (appDir, configName );
Try
{
_ ConfigDoc = XDocument. Load (_ path );

}
Catch (FileNotFoundException)
{
ConfigName = "Cp.Device.exe. Config ";
_ Path = Path. Combine (appDir, configName );
_ ConfigDoc = XDocument. Load (_ path );
}
Catch (Exception)
{
MessageBox. Show ("configuration file error ");
Return;
}

}

In this way, you can read the respective CONFIG files during the release and compilation, instead of having to modify the _ configName every time.

Now that we have obtained _ configDoc, let's talk about the other:

Operations on nodes

      private static XElement GetDescendant(string key)
{

var getAppSettings = _configDoc.Descendants("appSettings").Elements();
XElement elem = null;
foreach (var setting in getAppSettings)
{
XAttribute keyAtt = setting.Attribute("key");
if (keyAtt.Value == key)
{
elem = setting;
break;
}

}

return elem;

}

public static void Add(string key, string Value)
{
LoadConfigurationFile();
// LoadConfigurationFile(_configName);
XElement element = new XElement("add", new XAttribute("key", key), new XAttribute("value", Value));
var addElement = _configDoc.Descendants("appSettings").Elements().Last();
addElement.AddAfterSelf(element);
_configDoc.Save(_path);

}

public static string GetAppSettingValue(string key)
{
XElement getAppSettings = GetDescendant(key);
var configValue = getAppSettings.Attribute("value").Value;
return configValue.ToString();

}


public static void SetAppSettingValue(string key, string newValue)
{
XElement getAppSettings = GetDescendant(key);
getAppSettings.SetAttributeValue("value", newValue);
_configDoc.Save(_path);
}

It is also very easy to call.

var i = AppConfiger.GetAppSettingValue("DeviceSvcAddress");

So far, the Confier that we wrote can be used in CompactFramework has been completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.