-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileIO.cs
102 lines (87 loc) · 3.44 KB
/
FileIO.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ZXNTCount
{
class FileIO
{
public static bool TryOpenFile(Form owner, string initialDirectory, string initialFileName, string fileFormat, string[] extensions, out string fileName)
{
fileName = null;
try
{
OpenFileDialog fd = new OpenFileDialog();
fd.Title = "Open File";
fd.InitialDirectory = initialDirectory;
fd.FileName = initialFileName;
fd.Filter = String.Format("{0} ({1})|*{2}|All Files (*.*)|*.*", fileFormat, String.Join(",", extensions).Replace(".", "").ToUpper(), String.Join(";*", extensions));
fd.RestoreDirectory = true;
fd.CheckFileExists = true;
if (fd.ShowDialog(owner) == DialogResult.OK)
{
fileName = fd.FileName;
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
public static bool TrySaveFile(Control parent, string initialDirectory, string initialFileName, string fileFormat, string[] extensions, out string fileName)
{
fileName = null;
try
{
SaveFileDialog fd = new SaveFileDialog();
fd.Title = "Save Layout";
fd.InitialDirectory = initialDirectory;
fd.FileName = initialFileName;
fd.Filter = String.Format("{0} ({1})|*{2}|All Files (*.*)|*.*", fileFormat, String.Join(",", extensions).Replace(".", "").ToUpper(), String.Join(";*", extensions));
fd.OverwritePrompt = false;
fd.RestoreDirectory = true;
if (fd.ShowDialog(parent) == DialogResult.OK)
{
fileName = fd.FileName;
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
public static void ReadFormState(string fileName, Form form)
{
using (IniFile iniFile = new IniFile(fileName))
{
form.Size = iniFile.Read<Size>(form.Name, "Size", form.Size);
form.Location = iniFile.Read<Point>(form.Name, "Location", form.Location);
form.WindowState = iniFile.Read<FormWindowState>(form.Name, "WindowState", form.WindowState);
}
}
public static void WriteFormState(string fileName, Form form)
{
using (IniFile iniFile = new IniFile(fileName))
{
iniFile.Write<FormWindowState>(form.Name, "WindowState", form.WindowState);
if (form.WindowState == FormWindowState.Normal)
{
iniFile.Write<Size>(form.Name, "Size", form.Size);
iniFile.Write<Point>(form.Name, "Location", form.Location);
}
else
{
iniFile.Write<Size>(form.Name, "Size", form.RestoreBounds.Size);
iniFile.Write<Point>(form.Name, "Location", form.RestoreBounds.Location);
}
}
}
}
}