-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAccess.cs
More file actions
74 lines (63 loc) · 1.75 KB
/
DataAccess.cs
File metadata and controls
74 lines (63 loc) · 1.75 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;
using Cardprint.Models;
using static System.Net.Mime.MediaTypeNames;
namespace Cardprint;
public static class DataAccess
{
public static List<string> GetLayoutNames(string path)
{
var layoutNames = new List<string>();
try
{
var files = Directory.GetFiles(path, "*.xml");
foreach (var file in files)
{
layoutNames.Add(Path.GetFileNameWithoutExtension(file));
}
return layoutNames;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return layoutNames;
}
}
public static Layout? LoadLayout(string dirpath, string layoutName, out string error)
{
error = string.Empty;
var path = Path.Combine(dirpath, layoutName + ".xml");
if (!File.Exists(path))
{
error = $"File does not exist ({path})";
return null;
}
try
{
using (var stream = File.OpenRead(path))
{
var xmlSerializer = new XmlSerializer(typeof(Layout));
var layoutfile = xmlSerializer.Deserialize(stream) as Layout;
if(layoutfile is null)
{
error = "Deserialization failed";
return null;
}
layoutfile.Name = layoutName;
return layoutfile;
}
}
catch (Exception ex)
{
error = ex.Message;
return null;
}
}
}