#region Namespace using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; using System.Threading; using System.Xml.Serialization; #endregion namespace com.itac.mes.tools { public class DeserializedEventArgs { public DeserializedEventArgs(object deserializedObject) { DeserializedObject = deserializedObject; } public object DeserializedObject { get; set; } } /// /// Observes the directory local ./xml. /// Whenever a new file (only *.xml files) was read the content is interpreted as xml. /// The (simplified) first tag is treated as a simple typeName. If this simpleType name was /// added as a type to the local map (typeDict) the xml stream will be deserialized. /// If deserialisation was successful the instance is reported to the event handler. /// /// public class DirectoryMonitor { private bool _isShutdown = false; private Dictionary typeDict; public delegate void DeserializedObjectEventHandler(object sender, object deserializedObject); public event DeserializedObjectEventHandler deserializedObjectEvent; protected void raiseObjectDeserialized(object deserializedObject) { if (deserializedObjectEvent != null) deserializedObjectEvent(this, deserializedObject); } public void addType(Type typeToSerialize) { if (typeDict == null) { typeDict = new Dictionary(); } typeDict.Add(typeToSerialize.Name, typeToSerialize); } public void Start() { var thread = new Thread(Run); thread.Start(); } public void Run() { Thread.CurrentThread.Name = "DirectoryMonitor"; while (!_isShutdown) { try { // Directory.GetFiles() returns an array of strings which are not just // the file/directory name but the whole path to that file folder. string[] fileList = Directory.GetFiles("./xml"); for (int i = 0; i < fileList.Length; i++) { if (File.Exists(fileList[i])) { //File Info is a Class which extend FileSystemInfo class. FileInfo finfo = new FileInfo(fileList[i]); //finfo.Length returns the File size. if (fileList[i].ToLower().EndsWith("xml")) { LogHandler.log(Constants.LOGGER, TraceEventType.Information, "FILE: " + fileList[i] + " :Size>" + finfo.Length); if (File.Exists(fileList[i])) { using (StreamReader myFile = new StreamReader(fileList[i], System.Text.Encoding.Default)) { string sContent = myFile.ReadToEnd(); // solange weiter lesen, bis eine Zeile erkannt wird, die nicht mit ") - 1); if (typeDict.ContainsKey(typeName)) { Type type = typeDict[typeName]; XmlSerializer serializer = new XmlSerializerFactory().CreateSerializer(type); object deserializedObject = serializer.Deserialize(reader); raiseObjectDeserialized(deserializedObject); } } catch (Exception e) { LogHandler.log(Constants.LOGGER, TraceEventType.Error, "error processing xml file, ", e); } } } finfo.Delete(); } else { LogHandler.log(Constants.LOGGER, TraceEventType.Information, "FILE: " + fileList[i] + " has unsupported extension"); } } } Thread.Sleep(1000); } catch (Exception e) { LogHandler.log(Constants.LOGGER, TraceEventType.Error, "error processing xml file, " + e.Message); Thread.Sleep(1000); } } } private string getFileContent(string v) { string sContent = ""; try { } catch (Exception) { } return sContent; } } }