使用AppDomain的一个Demo,方便自编的Windows服务程序回收内存资源 public class AppDomainDemo { static void MainRun() { Type ftptype = typeof(RemoteLoader); //应用程序域的友好名称 string callingDomainName = System.Threading.Thread.GetDomain().FriendlyName; //程序集名称 string exeAssembly = ftptype.Assembly.FullName; AppDomainSetup ads = new AppDomainSetup(); ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; ads.DisallowBindingRedirects = false; ads.DisallowCodeDownload = true; ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; AppDomain ad2 = AppDomain.CreateDomain(callingDomainName, null, ads); //ObjectHandle objectHandle = ad2.CreateInstance(exeAssembly, tp.FullName); //MarshalByRefType mbrt = (MarshalByRefType)objectHandle.Unwrap(); RemoteLoader mbrt = (RemoteLoader)ad2.CreateInstanceAndUnwrap( exeAssembly, ftptype.FullName ); mbrt.Run(); AppDomain.Unload(ad2); } } public class RemoteLoader : MarshalByRefObject { public void Run() { FTPDownFile downFile; int ftpNum = Configuration.GetOption("ftpNum").ToString() == "" ? 0 : int.Parse(Configuration.GetOption("ftpNum").ToString()); string ftp; for (int i = 0; i < ftpNum; i++) { ftp = "ftp" + (i + 1); downFile = new FTPDownFile( Configuration.GetAttribute(ftp, "ServerIP"), Configuration.GetAttribute(ftp, "UserID"), Configuration.GetAttribute(ftp, "PassWord") ); string[] fileList = downFile.GetFileList(Configuration.GetAttribute(ftp, "ServerPath")); if (fileList != null && fileList.Length > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (string s in fileList) sb.Append(s + ","); Console.WriteLine("获取FTP文件列表," + sb.ToString().TrimEnd(",".ToCharArray()) + "准备下载"); string filePath = Configuration.GetOption("LocalPath"); string ftpfilePath = downFile.ftpServerIP + "/" + Configuration.GetAttribute(ftp, "ServerPath") + "/"; foreach (string fileName in fileList) { try { if (System.Text.RegularExpressions.Regex.IsMatch(fileName.ToLower(), @"/.zip|/.rar")) { downFile.Download(ftpfilePath + fileName, filePath); //downFile.SaveFile(filePath + fileName); } } catch { continue; } } } } } } /// <summary> /// 读取和操作工具的配置文件 /// </summary> public class Configuration { private static string configFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "/Config.xml"; public Hashtable options = new Hashtable(); public Configuration() { if (!File.Exists(configFilePath)) { throw new Exception("Config.xml配置文件不存在"); } XmlDocument xDoc = new XmlDocument(); xDoc.Load(configFilePath); XmlNodeList nodes = xDoc.SelectNodes("/ftpConfig/*"); for (int i = 0; i < nodes.Count; i++) { options.Add(nodes[i].Name, nodes[i].InnerText); } } public static string GetOption(string key) { if (!File.Exists(configFilePath)) { throw new Exception("Config.xml配置文件不存在"); } XmlDocument xDoc = new XmlDocument(); xDoc.Load(configFilePath); XmlNode node = xDoc.SelectSingleNode("/ftpConfig/" + key); return node.InnerText; } public static string GetAttribute(string ftp, string key) { if (!File.Exists(configFilePath)) { throw new Exception("Config.xml配置文件不存在"); } XmlDocument xDoc = new XmlDocument(); xDoc.Load(configFilePath); XmlNode node = xDoc.SelectSingleNode("/ftpConfig/" + ftp); return node.Attributes[key].Value; } } xml文件 <?xml version="1.0" encoding="utf-8" ?> <ftpConfig> <ftpNum>2</ftpNum> <ftp2 ServerIP="203.156.213.54" UserID="zhang_mh" PassWord="channel" ServerPath="www" /> <ftp1 ServerIP="203.156.213.54" UserID="zhang_mh" PassWord="channel" ServerPath="backuprar" /> <LocalPath>F://</LocalPath> <Time>07:30</Time> <!--开始运行--> <Span>4</Span> <!--间隔(小时)--> </ftpConfig>