⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmxmlconfig.cs

📁 移动设备的 LINQ 编程介绍 .NET Compact Framework 版 LINQ 的特性
💻 CS
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Reflection;

namespace Demo2___LinqToXml
{
	public partial class FrmXmlConfig : Form
	{
		private XDocument configFile;
		private string appPath;

		public FrmXmlConfig()
		{
			InitializeComponent();

			appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
		}

		private void mniLoadConfig_Click(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			txtWebServiceUrl.Text = GetAppSetting("WebServiceUrl");
			txtSSCDatabase.Text = GetAppSetting("SqlCeDatabase");
			txtSSCPassword.Text = GetAppSetting("SqlCePassword");

			Cursor.Current = Cursors.Default;
		}

		private void mniSaveConfig_Click(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			SetAppSetting("WebServiceUrl", txtWebServiceUrl.Text.Trim());
			SetAppSetting("SqlCeDatabase", txtSSCDatabase.Text.Trim());
			SetAppSetting("SqlCePassword", txtSSCPassword.Text.Trim());
			configFile.Save(appPath + @"\LinqToXml.exe.config");

			Cursor.Current = Cursors.Default;

			MessageBox.Show("Successful");
		}

		/// <summary>
		/// Retrieves setting from the xml config file
		/// </summary>
		/// <param name="appSettingName">setting to retrieve</param>
		/// <returns>value of the setting or empty string if not found</returns>
		private string GetAppSetting(string appSettingName)
		{
			try
			{
				return GetAppSettingNode(appSettingName).Attribute("value").Value;
			}
			catch (Exception)
			{
				return string.Empty;
			}
		}

		private void SetAppSetting(string appSettingName, string value)
		{
			try
			{
				GetAppSettingNode(appSettingName).Attribute("value").Value = value;
			}
			catch (Exception)
			{
			}
		}


		/// <summary>
		/// Retrieves the matching xml element from the xml config file
		/// </summary>
		/// <param name="appSettingName">setting to retrieve</param>
		/// <returns>the node containing the requested setting</returns>
		private XElement GetAppSettingNode(string appSettingName)
		{
			if (configFile == null)
			{
				configFile = XDocument.Load(appPath + @"\LinqToXml.exe.config");
			}
			var appSettings = (from appSetting in configFile.Descendants("add")
							   where appSetting.Attribute("key") != null &&
										 appSetting.Attribute("key").Value == appSettingName
							   select appSetting).Single();
			return appSettings;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -