-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSavingSettings.cs
213 lines (202 loc) · 7.54 KB
/
FileSavingSettings.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Linq;
namespace _163AlbumGet
{
public partial class FileSavingSettings : Form
{
public FileSavingSettings()
{
InitializeComponent();
}
public static string FileNameFilter(string s, string replacestring = null)
{
char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
string a = string.Empty;
foreach (char c in s)
{
if (InvalidFileNameChars.Contains(c))
{
if (replacestring is null) continue;
else a += replacestring;
}
else a += c;
}
return a;
}
public static string PathFilter(string s, string replacestring = null)
{
char[] InvalidPathChars = Path.GetInvalidPathChars().Concat(new char[] { '*', '?' }).ToArray();
string a = string.Empty;
bool n = false;
foreach (char c in s)
{
if (c == ':')
{
if (!n)
{
n = true;
a += c;
}
else
{
if (replacestring is null) continue;
else a += replacestring;
}
}
else if (InvalidPathChars.Contains(c))
{
if (replacestring is null) continue;
else a += replacestring;
}
else a += c;
}
return a;
}
private bool ReplaceCHK(string i, string d)
{
if (!i.Contains("&"))
{
return true;
}
int a = i.IndexOf("&");
i = i.Substring(a + 1);
int b = i.IndexOf(";");
if (b > 0)
{
i = i.Substring(0, b);
switch (i)
{
case "d":
case "aa":
case "at":
case "ai":
case "st":
case "si":
case "&":
break;
default:
Error("文件替换符错误,请检查是否符合替换规则!", "错误地点:" + d);
return false;
}
return true;
}
else
{
Error("文件替换符错误,请检查是否符合替换规则!", "错误地点:" + d);
return false;
}
}
private void Error(string s,string t)
{
Err.ForeColor = Color.Red;
Err.Text = s;
ErrLoc.ForeColor = Color.Red;
ErrLoc.Text = t;
}
private void DefaultFSD_Click(object sender, EventArgs e)
{
FileSaveDirI.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\";
}
private void DefaultAFN_Click(object sender, EventArgs e)
{
AlbumFolderNameI.Text = "&at;";
}
private void DefaultSSF_Click(object sender, EventArgs e)
{
SingleSongFilenameI.Text = "&st;";
}
private void DefaultMSF_Click(object sender, EventArgs e)
{
MultipleSongsFilenameI.Text = "&d;_&st;";
}
private void Browse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog
{
Description = "请选择文件夹路径"
};
switch (dialog.ShowDialog())
{
case DialogResult.OK:
FileSaveDirI.Text = dialog.SelectedPath;
break;
}
}
private void Confirm_Click(object sender, EventArgs e)
{
FileSaveDirI.Text = PathFilter(FileSaveDirI.Text);
AlbumFolderNameI.Text = FileNameFilter(AlbumFolderNameI.Text);
SingleSongFilenameI.Text = FileNameFilter(SingleSongFilenameI.Text);
MultipleSongsFilenameI.Text = FileNameFilter(MultipleSongsFilenameI.Text);
if (!(FileSaveDirI.Text.EndsWith("/") || FileSaveDirI.Text.EndsWith(@"\")))
{
FileSaveDirI.Text += @"\";
}
if (FileSaveDirI.Text != string.Empty ||
AlbumFolderNameI.Text != string.Empty ||
SingleSongFilenameI.Text != string.Empty ||
MultipleSongsFilenameI.Text != string.Empty)
{
if (ReplaceCHK(FileSaveDirI.Text, "文件保存地址 文本框") &&
ReplaceCHK(AlbumFolderNameI.Text, "专辑文件夹名称 文本框") &&
ReplaceCHK(SingleSongFilenameI.Text, "歌曲文件名称(单个下载) 文本框") &&
ReplaceCHK(MultipleSongsFilenameI.Text, "歌曲文件名称(批量下载) 文本框"))
{
Program.afn = AlbumFolderNameI.Text;
Program.ssf = SingleSongFilenameI.Text;
Program.msf = MultipleSongsFilenameI.Text;
IniFiles ini = new IniFiles(Program.tloc + @"\settings.ini");
ini.IniWriteValue("163AlbumGet", "AlbumFolderName", Program.afn);
ini.IniWriteValue("163AlbumGet", "SingleSongFilename", Program.ssf);
ini.IniWriteValue("163AlbumGet", "MultipleSongsFilename", Program.msf);
if (!Directory.Exists(FileSaveDirI.Text))//如果不存在就创建 dir 文件夹
{
DialogResult dr = MessageBox.Show("路径不存在!是否创建路径?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr == DialogResult.OK)
{
Directory.CreateDirectory(FileSaveDirI.Text);
Program.savedir = FileSaveDirI.Text;
ini.IniWriteValue("163AlbumGet", "FileSaveDir", Program.savedir);
Close();
}
else
{
Program.savedir = FileSaveDirI.Text;
ini.IniWriteValue("163AlbumGet", "FileSaveDir", Program.savedir);
Close();
}
}
else
{
Program.savedir = FileSaveDirI.Text;
ini.IniWriteValue("163AlbumGet", "FileSaveDir", Program.savedir);
Close();
}
}
}
else
{
Error("文件名不能为空!", string.Empty);
}
}
private void Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void FileSavingSettings_Load(object sender, EventArgs e)
{
FileSaveDirI.Text = Program.savedir;
AlbumFolderNameI.Text = Program.afn;
SingleSongFilenameI.Text = Program.ssf;
MultipleSongsFilenameI.Text = Program.msf;
}
private void AboutButton_Click(object sender, EventArgs e)
{
AboutPage f = new AboutPage();
f.ShowDialog();
}
}
}