13 Dateien lesen


01 Textdatei lesen
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _13_Dateien_lesen_01_Textdatei_lesen
{
  [Start]
  public void Function()
  {
    string fileName = PathMap.SubstitutePath(@"$(TMP)\Last_Version.txt");

    LabellingText(fileName);

    string lastVersion = ReadLine(fileName, 0);

    MessageBox.Show(
        "Zuletzt verwendete EPLAN-Version:" + Environment.NewLine +
        lastVersion,
        "Information",
        MessageBoxButtons.OK,
        MessageBoxIcon.Information
        );

    File.Delete(fileName);

    return;
  }

  public string ReadLine(string fileName, int line)
  {
    string[] lines = File.ReadAllLines(fileName, Encoding.Unicode);
    string text = lines[line];
    return text;
  }

  private static void LabellingText(string fileName)
  {
    ActionCallingContext acc = new ActionCallingContext();
    acc.AddParameter("CONFIGSCHEME", "Zuletzt verwendete EPLAN-Version_Textdatei");
    acc.AddParameter("DESTINATIONFILE", fileName);
    acc.AddParameter("FILTERSCHEME", "");
    acc.AddParameter("LANGUAGE", "de_DE");
    acc.AddParameter("LogMsgActionDone", "true");
    acc.AddParameter("SHOWOUTPUT", "0");
    acc.AddParameter("RECREPEAT", "1");
    acc.AddParameter("SORTSCHEME", "");
    acc.AddParameter("TASKREPEAT", "1");
    new CommandLineInterpreter().Execute("label", acc);

    return;
  }
}


02 XML-Datei lesen
using System;
using System.Windows.Forms;
using System.Xml;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _13_Dateien_lesen_02_XML_Datei_lesen
{
  [Start]
  public void Function()
  {
    string fileName = PathMap.SubstitutePath("$(PROJECTPATH)" + @"\"
                                              + "Projectinfo.xml");
    int id = 10043;
    string lastVersion = ReadProjectProperty(fileName, id);

    if (lastVersion != null)
    {
      MessageBox.Show(
          "Zuletzt verwendete EPLAN-Version:" + Environment.NewLine +
          lastVersion,
          "Information",
          MessageBoxButtons.OK,
          MessageBoxIcon.Information
          );
    }
    else
    {
      MessageBox.Show(
        "Eigenschaft " + id + " nicht gefunden.",
        "Warnung",
        MessageBoxButtons.OK,
        MessageBoxIcon.Warning
      );
    }

    return;
  }

  private static string ReadProjectProperty(string fileName, int id)
  {
    string property = null;

    XmlTextReader reader = new XmlTextReader(fileName);

    while (reader.Read())
    {
      if (reader.HasAttributes)
      {
        while (reader.MoveToNextAttribute())
        {
          if (reader.Name == "id")
          {
            if (reader.Value == id.ToString())
            {
              property = reader.ReadString();
            }
          }
        }
      }
    }

    reader.Close();
    return property;
  }
}


03 XML Datei lesen eigene Klasse
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

public class _12_Dateien_lesen_05_XML_Datei_lesen_eigene_Klasse
{
  [Start]
  public void Function()
  {
    string xmlPath = PathMap.SubstitutePath("$(MD_XML)");
    string fileName = Path.Combine(xmlPath, "Persons.xml");

    List<Person> persons = ReadXml(fileName);
    foreach (Person person in persons)
    {
      MessageBox.Show(person.FullName);
    }

    return;
  }

  public static List<Person> ReadXml(string fileName)
  {
    XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
    StreamReader reader = new StreamReader(fileName);

    List<Person> persons = (List<Person>)serializer.Deserialize(reader);
    reader.Close();
    reader.Dispose();
    return persons;
  }

  public class Person
  {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public AddressClass Address { get; set; }

    public string FullName
    {
      get { return FirstName + " " + LastName; }
    }

    public Person()
    {

    }

    public class AddressClass
    {
      [XmlAttribute]
      public string City { get; set; }

      [XmlAttribute]
      public string Street { get; set; }
    }
  }
}