-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
390 lines (356 loc) · 13.4 KB
/
MainForm.cs
File metadata and controls
390 lines (356 loc) · 13.4 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FtpClient
{
public partial class MainForm : Form
{
private FtpHelper ftpHelper;
private DateTime clickTime;
private bool isClicked = false;
public MainForm()
{
InitializeComponent();
}
#region 初始化加载事件
/// <summary>
/// 初始化界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
InitTreeView();
}
private void InitTreeView()
{
string[] drives = Environment.GetLogicalDrives();
int i = 0;
foreach (string drive in drives)
{
DriveInfo d = new DriveInfo(drive);
if ((d.DriveType & DriveType.Fixed) == DriveType.Fixed)
{
string drive1 = drive.Substring(0, drive.Length - 1);
this.treeLocal.Nodes[0].Nodes.Add(drive1);
this.treeLocal.Nodes[0].Nodes[i].ImageIndex = 1;
this.treeLocal.Nodes[0].Nodes[i].SelectedImageIndex = 1;
this.treeLocal.Nodes[0].Nodes[i].Tag = drive1;
this.treeLocal.Nodes[0].Nodes[i].Nodes.Add("");//增加一个空白节点,看起来是加号
i++;
}
}
}
#endregion
#region 菜单栏及工具栏
private void menuAbout_Click(object sender, EventArgs e)
{
AboutForm about = new AboutForm();
about.ShowDialog();
}
private void menuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void menuLogin_Click(object sender, EventArgs e)
{
this.btnLogin_Click(null, null);
}
/// <summary>
/// 前一个目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbtnPre_Click(object sender, EventArgs e)
{
//如果就目录,先设置上级目录,再列出目录
ftpHelper.SetPrePath();
this.ListDirectory();
}
#endregion
#region FTP操作功能
/// <summary>
/// 登录时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if (CheckInfo())
{
string ipAddr = this.txtAddress.Text.Trim();
string port = this.txtPort.Text.Trim();
string userName = this.txtUserName.Text.Trim();
string password = this.txtPassword.Text.Trim();
ftpHelper = new FtpHelper(ipAddr, port, userName, password);
ListDirectory();
}
}
catch (Exception ex) {
}
}
/// <summary>
/// 判断输入信息
/// </summary>
/// <returns></returns>
private bool CheckInfo()
{
string ipAddr = this.txtAddress.Text.Trim();
string port = this.txtPort.Text.Trim();
string userName = this.txtUserName.Text.Trim();
string password = this.txtPassword.Text.Trim();
if (string.IsNullOrEmpty(ipAddr) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
{
MessageBox.Show("请输入登录信息");
return false;
}
return true;
}
/// <summary>
/// 双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTmp_DoubleClick(object sender, EventArgs e)
{
Button btnTmp = (Button)sender;
FtpContentType contentType;
if (Enum.TryParse(btnTmp.Tag.ToString(), out contentType)) {
switch (contentType) {
case FtpContentType.folder:
//如果是目录
ftpHelper.RelatePath = string.Format("{0}/{1}", ftpHelper.RelatePath, btnTmp.Text);
this.ListDirectory();//再次列出目录
break;
default:
MessageBox.Show("非目录,请点击右键","提示",MessageBoxButtons.OK);
break;
}
}
}
/// <summary>
/// 列出目录
/// </summary>
private void ListDirectory()
{
bool isOk = false;
string[] arrAccept = ftpHelper.ListDirectory(out isOk);//调用Ftp目录显示功能
if (isOk)
{
this.flowPnlLeft.Controls.Clear();
foreach (string accept in arrAccept)
{
string name = accept.Substring(39);
Button btnTmp = new Button()
{
BackColor = Color.White,
TextImageRelation = TextImageRelation.ImageAboveText,
Text = name,
Width = 80,
Height = 80
};
btnTmp.FlatAppearance.BorderSize = 0;
btnTmp.FlatStyle = FlatStyle.Flat;
if (accept.IndexOf("<DIR>") != -1)
{
btnTmp.Image = global::FtpClient.Properties.Resources.folder.ToBitmap();
btnTmp.Tag = FtpContentType.folder;
}
else {
btnTmp.Image = global::FtpClient.Properties.Resources.file.ToBitmap();
btnTmp.Tag = FtpContentType.file;
btnTmp.ContextMenuStrip = menuRightKey;
btnTmp.MouseDown += new MouseEventHandler(BtnTmp_MouseDown);
}
btnTmp.Click += new EventHandler(btnTmp_Click);//此处由于双击事件被屏蔽,所以用单击事件模拟双击
this.flowPnlLeft.Controls.Add(btnTmp);
}
lblMsg.Text = "列出目录成功";
}
else {
ftpHelper.SetPrePath();
lblMsg.Text = "链接失败,或者没有数据";
}
}
private void BtnTmp_MouseDown(object sender, MouseEventArgs e)
{
//如果是右键按下
if (e.Button == MouseButtons.Right)
{
Button btnTmp = (Button)sender;
string name = btnTmp.Text;
this.menuSaveAs.Tag = name;
this.menuDelete.Tag = name;
}
}
private void btnTmp_Click(object sender, EventArgs e)
{
if (isClicked)
{
TimeSpan span = DateTime.Now - clickTime;
if (span.Milliseconds < SystemInformation.DoubleClickTime)
{
btnTmp_DoubleClick(sender, e);
isClicked = false;
}
}
else
{
isClicked = true;
clickTime = DateTime.Now;
}
}
#endregion
#region 树菜单事件
/// <summary>
/// 展开之前操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeLocal_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Level > 0) {
//点击之前,先填充节点:
string path = e.Node.FullPath.Substring(e.Node.FullPath.IndexOf("\\")+1)+"\\";
e.Node.Nodes.Clear();
//string path = string.Format("{0}{1}",e.Node.Tag,this.treeLocal.PathSeparator);
string[] files= Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
int i = 0;
foreach (string file in files) {
FileInfo f = new FileInfo(file);
if ((f.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden && (f.Attributes & FileAttributes.System) != FileAttributes.System)
{
e.Node.Nodes.Add(Path.GetFileName(file));
e.Node.Nodes[i].ImageIndex = 3;
e.Node.Nodes[i].SelectedImageIndex = 3;
e.Node.Nodes[i].Tag = file;
i++;
}
}
string[] dirs= Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
foreach (string dir in dirs)
{
DirectoryInfo d = new DirectoryInfo(dir);
if ((d.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden && (d.Attributes & FileAttributes.System) != FileAttributes.System)
{
e.Node.Nodes.Add(Path.GetFileName(dir));
e.Node.Nodes[i].ImageIndex = 2;
e.Node.Nodes[i].SelectedImageIndex = 2;
e.Node.Nodes[i].Tag = dir;
e.Node.Nodes[i].Nodes.Add("");//增加一个空白节点,看起来是加号
i++;
}
}
}
}
/// <summary>
/// 选中后,右键事件判断,只有文件类型,右键才可用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeLocal_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Level > 0 && e.Node.ImageIndex == 3)
{
this.menuLocal.Enabled = true;
string path = e.Node.FullPath.Substring(e.Node.FullPath.IndexOf("\\") + 1);
this.menuLoad.Tag = path;
}
else {
this.menuLocal.Enabled = false;
}
}
#endregion
#region 右键事件
/// <summary>
/// 右键另存为按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuSaveAs_Click(object sender, System.EventArgs e)
{
ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
string name = menuItem.Tag.ToString();
SaveFileDialog sfd = new SaveFileDialog()
{
FileName = name,
Filter="All Files|(*.*)",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Title="另存为"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
//下载文件
string filePath = sfd.FileName;
string fileName = Path.GetFileName(filePath);
ftpHelper.RelatePath = string.Format("{0}/{1}", ftpHelper.RelatePath, fileName);
bool isOk = false;
ftpHelper.DownLoad(filePath, out isOk);
if (isOk)
{
lblMsg.Text = "下载成功";
}
else {
lblMsg.Text = "下载失败";
}
ftpHelper.SetPrePath();
}
}
/// <summary>
/// 右键删除功能FTP上的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuDelete_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
string name = menuItem.Tag.ToString();
ftpHelper.RelatePath = string.Format("{0}/{1}", ftpHelper.RelatePath, name);
bool isOk = false;
ftpHelper.DeleteFile(out isOk);
ftpHelper.SetPrePath();
if (isOk)
{
this.ListDirectory();
lblMsg.Text = "删除成功";
}
else {
lblMsg.Text = "删除失败";
}
}
/// <summary>
/// 本地文件上传事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuLoad_Click(object sender, EventArgs e)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
string path = mi.Tag.ToString();
if (File.Exists(path)) {
ftpHelper.RelatePath = string.Format("{0}/{1}", ftpHelper.RelatePath, Path.GetFileName(path));
bool isOk = false;
ftpHelper.UpLoad(path,out isOk);
ftpHelper.SetPrePath();
if (isOk)
{
this.ListDirectory();
lblMsg.Text = "上传成功";
}
else {
lblMsg.Text = "上传失败";
}
}
}
#endregion
}
}