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

📄 configmanagement.cs

📁 一个使用.NET Framework开发的、免费的、开发源码的下载管理程序.
💻 CS
字号:
/************************************************************

ConfigManagement.cs

Various functions for retrieving and modifying data in
filelist.xml, which stores application settings and
and details on unfinished jobs

Revision history:
	November 17 2000 by Joe Hardy
		- All code was split into modules from downloader.cs (now obsolete)
	November 26 2000 by Joe Hardy
		- Created new class, AppSettings, for retrieving and modifying data in the new
		  application config file, appconfig.xml
	Late June - 6 July, 2000 by Joe Hardy
		- Cut all the code across to work with beta 2

************************************************************/

using System;
using System.Xml;
using System.Xml.XPath;
using System.Collections;

public class JobData {
	public string url;
	public string localPath;
	public string localName;
	public bool nowDownloading=false;
}

// JobManagement class, used for maintaining the data found in filelist.xml. Please note that
// this class is *only* to be used with retrieving and modifying data in filelist.xml, files
// to do with the job should *not* be worked with here (ie, DeleteJob should not delete the
// local file that is referenced through localPath and localName
public class JobManagement {
	public static int CreateJob(JobData jobData) {
		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("..\\filelist.xml");
		XmlNode xn = xmldocument.DocumentElement;

		XmlNode newelement = xmldocument.CreateNode(XmlNodeType.Element, "file", "");

		XmlAttribute xa;
		xa = xmldocument.CreateAttribute("url");
		xa.Value = jobData.url;
		newelement.Attributes.InsertBefore(xa, null);
		xa = xmldocument.CreateAttribute("localpath");
		xa.Value = jobData.localPath;
		newelement.Attributes.InsertBefore(xa, null);
		xa = xmldocument.CreateAttribute("localname");
		xa.Value = jobData.localName;
		newelement.Attributes.InsertBefore(xa, null);
		xa = xmldocument.CreateAttribute("nowdownloading");
		xa.Value = jobData.nowDownloading.ToString();
		newelement.Attributes.InsertBefore(xa, null);

		xn.AppendChild(newelement);

		xmldocument.Save("..\\filelist.xml");

		return 0;
	}

	// return a job matching the passed url
	public static JobData GetJob(string url) {
		JobData jobData = new JobData();
		bool foundit=false;

		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("../filelist.xml");
		XmlNode xn = xmldocument.DocumentElement;

		xn = xn.FirstChild;

		if (xn!=null) {
			while (true) {
				if (xn.Attributes["url"].Value==url) {
					foundit=true;
					jobData.url = xn.Attributes["url"].Value;
					jobData.localPath = xn.Attributes["localpath"].Value;
					jobData.localName = xn.Attributes["localname"].Value;
					jobData.nowDownloading = (xn.Attributes["nowdownloading"].Value=="True");
					break;
				}

				if (xn.NextSibling == null)
					break;
				else
					xn = xn.NextSibling;
			}
		}

		if (!foundit) throw new ApplicationException("Could not find requested job");

		return jobData;
	}

	// modify a job
	public static int ModifyJob(JobData jobData, bool reloadlist) {
		bool foundit=false;

		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("../filelist.xml");
		XmlNode xn = xmldocument.DocumentElement;

		xn = xn.FirstChild;

		if (xn==null) return 0;

		while (true) {
			if (xn.Attributes["url"].Value==jobData.url) {
				foundit=true;
				xn.Attributes["url"].Value = jobData.url;
				xn.Attributes["localpath"].Value = jobData.localPath;
				xn.Attributes["localname"].Value = jobData.localName;
				xn.Attributes["nowdownloading"].Value = jobData.nowDownloading.ToString();
				xmldocument.Save("../filelist.xml");
				if (reloadlist) MainForm.LoadJobs(false);
				break;
			}

			if (xn.NextSibling == null)
				break;
			else
				xn = xn.NextSibling;
		}

		if (!foundit) return 1;

		return 0;
	}

	// gets all jobs out of filelist.xml and returns a ListItemCollection
	// to be used with a ListView control
	public static ArrayList GetAllJobs() {
		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("../filelist.xml");
		XmlNode xn = xmldocument.DocumentElement;

		xn = xn.FirstChild;

		ArrayList al = new ArrayList();

		if (xn==null) return al;

		while (true) {
			JobData jobData = new JobData();

			jobData.url = xn.Attributes["url"].Value;
			jobData.localPath = xn.Attributes["localpath"].Value;
			jobData.localName = xn.Attributes["localname"].Value;
			jobData.nowDownloading = (xn.Attributes["nowdownloading"].Value=="True");

			al.Add(jobData);

			if (xn.NextSibling == null)
				break;
			else
				xn = xn.NextSibling;
		}

		return al;
	}

	// nuke a job
	public static int DeleteJob(string url) {
		bool foundit=false;

		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("../filelist.xml");
		XmlNode xn = xmldocument.DocumentElement;

		xn = xn.FirstChild;

		if (xn==null) return 0;

		while (true) {
			if (xn.Attributes["url"].Value==url) {
				foundit=true;
				xn.ParentNode.RemoveChild(xn);
				xmldocument.Save("../filelist.xml");
				break;
			}

			if (xn.NextSibling == null)
				break;
			else
				xn = xn.NextSibling;
		}

		if (!foundit) return 1;

		return 0;
	}
}

public class AppSettings {
	public static string GetSetting(string key) {
		bool foundit=false;

		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("..\\appconfig.xml");
		XmlNode xn = xmldocument.DocumentElement;

		xn = xn.FirstChild;

		if (xn==null) return null;
		while (true) {
			if (xn.Attributes["key"] != null && xn.Attributes["key"].Value==key) {
				foundit=true;
				break;
			}

			if (xn.NextSibling == null) {
				break;
			} else {
				xn = xn.NextSibling;
			}
		}
		
		if (foundit)
			return xn.Attributes["value"].Value;
		else
			return null;
	}

	public static void SaveSetting(string key, string value) {
		bool foundit=false;
		XmlDocument xmldocument = new XmlDocument();
		xmldocument.Load("..\\appconfig.xml");
		XmlNode xn = xmldocument.DocumentElement;

		if (xn.FirstChild!=null && xn.FirstChild.Attributes.Count > 0) {
			xn = xn.FirstChild;
			while (true) {
				if (xn.Attributes["key"].Value==key) {
					foundit=true;
					xn.Attributes["value"].Value = value;
					break;
				}

				if (xn.NextSibling == null) {
					if (!foundit) xn = xn.ParentNode;
					break;
				} else {
					xn = xn.NextSibling;
				}
			}
		}

		if (!foundit) {
			XmlNode newelement = xmldocument.CreateNode(XmlNodeType.Element, "config", "");
			XmlAttribute xa = xmldocument.CreateAttribute("key");
			xa.Value = key;
			newelement.Attributes.InsertBefore(xa, null);
			xa = xmldocument.CreateAttribute("value");
			xa.Value = value;
			newelement.Attributes.InsertBefore(xa, null);
			xn.AppendChild(newelement);
		}

		xmldocument.Save("..\\appconfig.xml");

		return;
	}
}

⌨️ 快捷键说明

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