목표: ftp protocol을 이용하여 지정된 host로부터 파일 다운로드 하여보자.
public bool DownloadFile(string sourcefile, string localfile, bool IsfileSizeChk)
{
int fsize=0;
FileInfo fi = new FileInfo(localfile);
// 1. check source
string target;
if (sourcefile.Trim() == "")
{
throw (new ApplicationException("File not Specified!"));
}
else if (sourcefile.Contains("/"))
{
//treat as a full path
target = AdjustDir(sourcefile);
}
else
target = "/" + sourcefile;
string URI = "ftp://" + axSinfo.hostname + "/" + target;
// 2. perform copy
System.Net.FtpWebRequest ftp = GetRequest(URI);
// set requeset to download a file in binary mode
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;
// open request and get response stream
try
{
using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
// loop to read & write to file
using (FileStream fs = fi.OpenWrite())
{
try
{
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
fsize +=read;
} while (!(read == 0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch (Exception)
{
// catch error and delete file only partially dowmloaded
fs.Close();
// delete target files as it's incomplete
fi.Delete();
throw;
}
}
responseStream.Close();
}
response.Close();
// local PC로 download가 정상적으로 수행되었는지 file size를 Check한다.
CheckFileStatus(fsize, fi, IsfileSizeChk);
}
}
catch (WebException err)
{
string strErrMsg;
strErrMsg = string.Format(@"Window MSG:{0}
파일이 존재하는지 확인해 주십시요]
Download file name:{1} IP:{2}, USER ID:{3}]", err.ToString(),target, axSinfo.hostname, axSinfo.userid);
MessageBox.Show(strErrMsg,"Error Information!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ftp = null;
return true;
}
file이 존재하는지 확인한다.
private void CheckFileStatus(int fsize, FileInfo fi, bool IsfileSizeChk)
{
if (IsfileSizeChk == true)
{
string strTemp;
if (fi.Exists == false)
MessageBox.Show("TESTER SIDE에 파일이 존재하지 않습니다. 확인 바랍니다.", "Error Information",
MessageBoxButtons.OK, MessageBoxIcon.Error);
if (fsize != fi.Length)
{
strTemp = string.Format("파일이 정상적으로 다운로드 되지 않았습니다. filesize [{0}] : [{1}]",
fsize, fi.Length);
MessageBox.Show(strTemp, "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
/를 씌우고, user name password를 지정한다.
/// Amend an FTP path so that it always starts with
private string AdjustDir(string path)
{
return ((path.StartsWith("/")) ? "" : "/").ToString() + path;
}
//Get the basic FtpWebRequest object with the
//common settings and security
private FtpWebRequest GetRequest(string URI)
{
//create request
FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
//Set the login details
result.Credentials = GetCredentials();
//Do not keep alive (stateless mode)
result.KeepAlive = false;
return result;
}
/// <summary>
/// Get the credentials from username/password
/// </summary>
private System.Net.ICredentials GetCredentials()
{
return new System.Net.NetworkCredential(axSinfo.userid, axSinfo.password);
}
'프로그래밍 > .Net' 카테고리의 다른 글
서버PC에 있는 파일 실행시키기(host execute) (0) | 2014.01.17 |
---|---|
ftp protocol을 이용하여 지정된 host에 파일 업로드 하기(ftp upload) (0) | 2014.01.17 |
Control Attribute 컨트롤 속성 변경하기(get/set) (0) | 2014.01.17 |
외부응용프로그램 실행하기(Process.Start 메서드) (0) | 2014.01.17 |
Ping Test를 해보자 (0) | 2014.01.17 |