-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTPBackup.cs
111 lines (107 loc) · 3.71 KB
/
FTPBackup.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace MenuSMPTUserConsoleApp
{
class FTPBackup
{
#region
public string host = "ftp://82.214.114.2";
public string path = "/HomeWork/";
public string fileToUpload = "MyFile.txt";
public string userId = "bojan_academy";
public string password = "qjeK7#88";
public string filePath = "/HomeWork/HristijanFolder/";
public string deleteFile = "/HomeWork";
#endregion
//Create ftp folder
#region
public bool CreateFtpFolder()
{
string path = "/HomeWork/";
bool isCreated = true;
try
{
WebRequest request = WebRequest.Create(host + path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(userId, password);
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine("\t" + resp.StatusCode + " into your specified path!");
}
}
catch (WebException ex)
{
isCreated = false;
Console.WriteLine(ex.Message);
Console.WriteLine("\t Folder already exists!");
Console.WriteLine("\t Press any key to continue..");
Console.ReadLine();
}
return isCreated;
}
#endregion
//Delete directory
#region
public void DeleteFtpFolder()
{
try
{
WebRequest request = WebRequest.Create(host + path);
request.Method = WebRequestMethods.Ftp.RemoveDirectory;
request.Credentials = new NetworkCredential(userId, password);
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
#endregion
//Upload file
#region
public void FileToUpload()
{
string filePath = "/HomeWork/";
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(userId, password);
wc.UploadFile(host + filePath + fileToUpload, @"C:\temp\" + fileToUpload);
}
#endregion
//Check if the directory exists
#region
public bool DoesFtpDirectoryExist(string host, string userId, string password)
{
bool isexist = false;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host);
request.Credentials = new NetworkCredential(userId, password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
isexist = true;
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
return false;
}
}
}
return isexist;
}
#endregion
}
}