if(saveFtpInfo.Checked)
{
Properties.Settings.Default.FtpIpAddress = FTPIpAddrTxt.Text;
Properties.Settings.Default.FtpPort = FTPPortTxt.Text;
Properties.Settings.Default.FtpUserId = FTPUserIdTxt.Text;
Properties.Settings.Default.FtpUserPw = FTPUserPwTxt.Text;
Properties.Settings.Default.FtpSaveInfo = true;
}
else
{
Properties.Settings.Default.FtpSaveInfo = false;
}
Properties.Settings.Default.Save();
// 전체파일 불러오기
private List<DirectoryPath> getFTPList(string path)
{
string url = $@"FTP://{this.ipAddr}:{this.port}/{path}";
DirectoryPath directoryPath = null;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential(userId, pwd);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
using(StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{
string strData = reader.ReadToEnd();
if(string.IsNullOrEmpty(strData))
{
directoryPath = new DirectoryPath();
directoryPath.Folder = path;
directoryPath.File = "EMPTY";
directoryPaths.Add(directoryPath);
}
string[] filename = strData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string file in filename)
{
string[] fileDetailes = file.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
directoryPath = new DirectoryPath();
if (fileDetailes[0].Contains("d"))
{
getFTPList($"{path}{fileDetailes[8]}/");
}
else
{
directoryPath.Folder = path;
directoryPath.File = fileDetailes[8];
directoryPaths.Add(directoryPath);
}
//Console.WriteLine($"권한 : {fileDetailes[0]}");
//Console.WriteLine($"파일or폴더 : {fileDetailes[8]}");
}
return directoryPaths;
}
}
}
// menu item선택 이벤트
private void NavBarItem_CLick(object sender, NavBarLinkEventArgs e)
{
NavBarItem item = (NavBarItem)sender;
string Folder = $"/{item.Hint.Replace("/", "")}";
string File = (string.IsNullOrEmpty(item.Caption) || item.Caption.Equals("EMPTY")) ? "" : $"/{item.Caption}";
saveDirPath.Text = Folder;
saveFilePath.Text = File;
deleteFilePath.Text = $"{Folder}{File}";
}
// FTP 파일 삭제
public bool DeleteFTPFile(string path)
{
try
{
string url = $@"FTP://{this.ipAddr}:{this.port}/{path}";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential(userId, pwd);
if (path.Split('/').Count() > 2)
request.Method = WebRequestMethods.Ftp.DeleteFile;
else
request.Method = WebRequestMethods.Ftp.RemoveDirectory ;
using (request.GetResponse()) { }
}
catch
{
return false;
}
return true;
}
// 업로드 버튼
private void UploadBtn_Click(object sender, EventArgs e)
{
if(!result)
{
MessageBox.Show("FTP 연결을 먼저 진행해주세요.");
return;
}
if(openFileTxt.Text.Equals("Click choise File") || string.IsNullOrEmpty(openFileTxt.Text))
{
MessageBox.Show("업로드할 파일을 선택해주세요.");
return;
}
if (string.IsNullOrEmpty(uploadFolderTxt.Text) || !Regex.IsMatch(uploadFolderTxt.Text, @"(\/|\\){1}[a-zA-Z0-9ㄱ-ㅎ가-힣]+$"))
{
MessageBox.Show("업로드할 위치를 입력해주세요.\n(/폴더, /폴더/폴더 형식으로 입력해주세요.)");
return;
}
string fileName = openFileTxt.Text;
string path = uploadFolderTxt.Text.Replace('\\','/'); // 업로드할 파일 저장할 FTP 경로 지정
if (fTP.UpLoad(path, fileName) == false) // 파일 업로드
{
MessageBox.Show("FTP Upload 실패");
}
else
{
MessageBox.Show("FTP Upload시작");
MessageBox.Show("FTP Upload완료");
ResetMenuBar();
}
/*
// 기준 경로의 모든 파일을 업로드 할 때 사용
string localPath = @"C:\Users\jjh\Desktop\"; // 바탕화면 경로를 기준으로 둔다
DirectoryInfo dirinfo = new DirectoryInfo(localPath);
FileInfo[] infos = dirinfo.GetFiles();
foreach (FileInfo info in infos)
{
if (Path.GetExtension(info.FullName) == ".txt") // txt 확장자 파일만 FTP 서버에 Upload
{
if (fTP.UpLoad(path, info.FullName) == false) // 파일 업로드
{
MessageBox.Show("FTP Upload 실패");
}
else
{
MessageBox.Show("FTP Upload시작");
MessageBox.Show("FTP Upload완료");
}
}
}
*/
}