OfflineFuncs.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml;
6 using System.IO;
7 using System.Security.AccessControl;
8 using System.Security.Principal;
9 
10 namespace sage.addons.offline.Negocio.Clases
11 {
15  public static class clsFuncs
16  {
24  public static void Generate_Xml_Return(string tcNode, string tcReturn, string tcErrorMsg = "", string tcAttribute = "")
25  {
26  // Create the XmlDocument.
27  XmlDocument xmlConfiguracion = new XmlDocument();
28  XmlAttribute newElem;
29 
30  // Comprovem si existeix el directori de l'aplicació
31  string lcDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Sage.ew.offline";
32 
33  // PE-88858. Crea el directori amb permisos de lectura/escriptura
34  _CreateDirectory(lcDirectory);
35 
36  string lcFitxer = lcDirectory + @"\sage.ew.offline.xml";
37 
38  // Si el fitxer existeix, l'aprofitem, si no es crea de nou
39  if (!File.Exists(lcFitxer))
40  {
41  xmlConfiguracion.LoadXml("<?xml version = \"1.0\" encoding=\"Windows-1252\" standalone=\"yes\"?><returnvalues> </returnvalues>");
42  }
43  else
44  {
45  try
46  {
47  xmlConfiguracion.Load(lcFitxer);
48  }
49  catch (Exception)
50  {
51  xmlConfiguracion.LoadXml("<?xml version = \"1.0\" encoding=\"Windows-1252\" standalone=\"yes\"?><returnvalues> </returnvalues>");
52  }
53  }
54 
55  // Si existeix el node, l'esborrem
56  XmlNode loNodeA = xmlConfiguracion.SelectSingleNode("returnvalues");
57  XmlNode loNode = loNodeA.SelectSingleNode(tcNode);
58 
59  if (loNode != null)
60  if (!string.IsNullOrWhiteSpace(tcAttribute))
61  {
62  // Aprofitem el node
63  }
64  else
65  {
66  // El creem de nous
67  loNodeA.RemoveChild(loNode);
68  loNode = null;
69  }
70 
71  if (loNode == null)
72  {
73  // Afegim el nom de la classe
74  loNode = xmlConfiguracion.CreateElement(tcNode);
75  loNodeA.AppendChild(loNode);
76  }
77 
78  // Afegim els atributs
79  if (!string.IsNullOrWhiteSpace(tcAttribute))
80  {
81  // Creem un nou attribut
82  newElem = xmlConfiguracion.CreateAttribute(tcAttribute);
83  newElem.InnerText = tcReturn;
84  loNode.Attributes.Append(newElem);
85  }
86  else
87  {
88  // Atributs normals
89  newElem = xmlConfiguracion.CreateAttribute("value");
90  newElem.InnerText = tcReturn;
91  loNode.Attributes.Append(newElem);
92 
93  newElem = xmlConfiguracion.CreateAttribute("error_message");
94  newElem.InnerText = tcErrorMsg;
95  loNode.Attributes.Append(newElem);
96 
97  newElem = xmlConfiguracion.CreateAttribute("time_stamp");
98  newElem.InnerText = DateTime.Now.ToString();
99  loNode.Attributes.Append(newElem);
100  }
101 
102  // Save the document to a file. White space is preserved (no white space).
103  xmlConfiguracion.PreserveWhitespace = true;
104  xmlConfiguracion.Save(lcFitxer);
105  }
106 
114  public static string Get_Xml_Value(string tcNode, string tcAttribute, string tcTipoDatos = "String")
115  {
116  // Create the XmlDocument.
117  XmlDocument xmlConfiguracion = new XmlDocument();
118 
119  string lcValor;
120  switch (tcTipoDatos)
121  {
122  case "String":
123  lcValor = "";
124  break;
125 
126  case "Boolean":
127  lcValor = "false";
128  break;
129 
130  case "DataTime":
131  lcValor = null;
132  break;
133 
134  default:
135  lcValor = "";
136  break;
137  }
138 
139  // Comprovem si existeix el directori de l'aplicació
140  string lcDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Sage.ew.offline";
141 
142  // PE-88858. Crea el directori amb permisos de lectura/escriptura
143  _CreateDirectory(lcDirectory);
144 
145  string lcFitxer = lcDirectory + @"\sage.ew.offline.xml";
146 
147  // Si el fitxer existeix, l'aprofitem, si no es crea de nou
148  if (!File.Exists(lcFitxer))
149  {
150  return lcValor;
151  }
152  else
153  {
154  try
155  {
156  xmlConfiguracion.Load(lcFitxer);
157  }
158  catch (Exception)
159  {
160  return lcValor;
161  }
162  }
163 
164  // Si existeix el node, l'esborrem
165  XmlNode loNodeA = xmlConfiguracion.SelectSingleNode("returnvalues");
166  XmlNode loNode = loNodeA.SelectSingleNode(tcNode);
167 
168  if (loNode == null)
169  return lcValor;
170 
171  // Busco el valor de l'atrribut
172  if (loNode.Attributes[tcAttribute] != null)
173  lcValor = loNode.Attributes[tcAttribute].InnerText;
174 
175  return lcValor;
176  }
177 
183  public static void _CreateDirectory(string tcDirectory)
184  {
185  // Comprova si existeix el directory
186  System.IO.DirectoryInfo lDirInfo = new System.IO.DirectoryInfo(tcDirectory);
187  if (!lDirInfo.Exists)
188  {
189  // i el crea
190  Directory.CreateDirectory(tcDirectory);
191  }
192 
193  // Aplica permisos al directori
194  FileSystemAccessRule lFileSysAccess = new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow);
195  DirectorySecurity lDirSec = null;
196  lDirSec = lDirInfo.GetAccessControl();
197  lDirSec.AddAccessRule(lFileSysAccess);
198  lDirInfo.SetAccessControl(lDirSec);
199  }
200  }
201 }
Es como el tipo de entrada asientos pero por negocio, sin formulario, pq quiero que me haga las propu...