-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTPUtility.cs
105 lines (90 loc) · 2.85 KB
/
FTPUtility.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
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace ScreenGrab
{
class FTPUtility
{
private String host;
private String user;
private String pass;
private String tempname;
private String filename;
private FtpWebRequest req;
private UploadProgess prog;
public FTPUtility(String host, String user, String pass)
{
this.host = host;
this.user = user;
this.pass = pass;
}
public FTPUtility(String host, String user, String pass, String tempname, String filename)
{
this.host = host;
this.user = user;
this.pass = pass;
this.tempname = tempname;
this.filename = filename;
}
public void uploadFile()
{
prog = new ScreenGrab.UploadProgess();
prog.Show();
Thread t = new Thread(new ThreadStart(executeUploadFile));
t.Start();
}
private void executeUploadFile()
{
FileInfo file = new FileInfo(tempname);
String uri = "ftp://" + host + "/" + filename;
req = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
req.Credentials = new NetworkCredential(user, pass);
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = file.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = file.OpenRead();
try
{
Stream strm = req.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch (Exception)
{
MessageBox.Show("Image failed to upload.", "ScreenGrab");
}
prog.SafeInvoke(() => { prog.Close(); });
}
public bool test()
{
String uri = "ftp://" + host + "/";
req = (FtpWebRequest) FtpWebRequest.Create(new Uri(uri));
req.Credentials = new NetworkCredential(user, pass);
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.ListDirectory;
req.UseBinary = true;
try
{
WebResponse response = req.GetResponse();
response.Close();
}
catch (Exception)
{
return false;
}
return true;
}
}
}