-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLSaved.cs
131 lines (119 loc) · 4.47 KB
/
XMLSaved.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// C# (.Net Framework)
// dkxce.DllExportList
// v 0.1, 17.07.2023
// dkxce (https://github.com/dkxce/DllExportList)
// en,ru,1251,utf-8
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
namespace MSol
{
/// <summary>
/// KeyValuePair Serializable (XML, JSON, Binary)
/// </summary>
[Serializable]
[XmlType(TypeName = "kvp")]
public struct NameValue
{
/// <summary>
/// Parameter Name
/// </summary>
public string n { get; set; }
/// <summary>
/// Parameter Value
/// </summary>
public string v { get; set; }
public NameValue(string n, string v) { this.n = n; this.v = v; }
public override string ToString()
{
return n + ": " + v;
}
}
[Serializable]
public class XMLSaved<T>
{
/// <summary>
/// Сохранение структуры в файл
/// </summary>
/// <param name="file">Полный путь к файлу</param>
/// <param name="obj">Структура</param>
public static void Save(string file, T obj)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.StreamWriter writer = System.IO.File.CreateText(file);
xs.Serialize(writer, obj, ns);
writer.Flush();
writer.Close();
}
public static void SaveHere(string file, T obj)
{
Save(System.IO.Path.Combine(CurrentDirectory(), file), obj);
}
public static string Save(T obj)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.MemoryStream ms = new MemoryStream();
System.IO.StreamWriter writer = new StreamWriter(ms);
xs.Serialize(writer, obj, ns);
writer.Flush();
ms.Position = 0;
byte[] bb = new byte[ms.Length];
ms.Read(bb, 0, bb.Length);
writer.Close();
return System.Text.Encoding.UTF8.GetString(bb); ;
}
/// <summary>
/// Подключение структуры из файла
/// </summary>
/// <param name="file">Полный путь к файлу</param>
/// <returns>Структура</returns>
public static T Load(string file)
{
try
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.StreamReader reader = System.IO.File.OpenText(file);
T c = (T)xs.Deserialize(reader);
reader.Close();
return c;
}
catch { };
{
Type type = typeof(T);
System.Reflection.ConstructorInfo c = type.GetConstructor(new Type[0]);
return (T)c.Invoke(null);
};
}
public static T LoadHere(string file)
{
return Load(System.IO.Path.Combine(CurrentDirectory(), file));
}
public static T Load()
{
try { return Load(CurrentDirectory() + @"\config.xml"); }
catch { };
Type type = typeof(T);
System.Reflection.ConstructorInfo c = type.GetConstructor(new Type[0]);
return (T)c.Invoke(null);
}
public static string CurrentDirectory()
{
return AppDomain.CurrentDomain.BaseDirectory;
// return Application.StartupPath;
// return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
// return System.IO.Directory.GetCurrentDirectory();
// return Environment.CurrentDirectory;
// return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
// return System.IO.Path.GetDirectory(Application.ExecutablePath);
}
}
}