最近用到了ftp功能,把上传代码写下来做个记号。
用的是support.dll,从 这里下载。
用的时候需要先引用这个dll,然后using FtpSupport;
下面是ftp上传代码:
用的是support.dll,从 这里下载。
用的时候需要先引用这个dll,然后using FtpSupport;
下面是ftp上传代码:
/// <summary>
/// ftp上传
/// </summary>
/// <param name="localFile">形如 Server.MapPath("~/")+"log.txt"</param>
/// <param name="dr">ftp服务器端目录名称,形如/vagerent/image/</param>
/// <param name="newRemoteFile">传到服务器后的文件名称,比如log.txt</param>
/// <returns></returns>
private int FtpFile(string localFile, string dr, string newRemoteFile)
{
string remoteDr = "/";
FtpConnection ftp = new FtpConnection();
try
{
string ftpIP = ConfigurationSettings.AppSettings["ftpIP"];
string ftpUsrName = ConfigurationSettings.AppSettings["ftpUsrName"];
string ftpUsrPsw = ConfigurationSettings.AppSettings["ftpUsrPsw"];
ftp.Connect(ftpIP, ftpUsrName, ftpUsrPsw);
remoteDr = dr;
ftp.SetCurrentDirectory(remoteDr);
ftp.PutFile(localFile, newRemoteFile);
return 1;//上传成功
}
catch (Exception err)
{
return 0;//上传失败
}
finally
{
ftp.Close();
}
}
/// ftp上传
/// </summary>
/// <param name="localFile">形如 Server.MapPath("~/")+"log.txt"</param>
/// <param name="dr">ftp服务器端目录名称,形如/vagerent/image/</param>
/// <param name="newRemoteFile">传到服务器后的文件名称,比如log.txt</param>
/// <returns></returns>
private int FtpFile(string localFile, string dr, string newRemoteFile)
{
string remoteDr = "/";
FtpConnection ftp = new FtpConnection();
try
{
string ftpIP = ConfigurationSettings.AppSettings["ftpIP"];
string ftpUsrName = ConfigurationSettings.AppSettings["ftpUsrName"];
string ftpUsrPsw = ConfigurationSettings.AppSettings["ftpUsrPsw"];
ftp.Connect(ftpIP, ftpUsrName, ftpUsrPsw);
remoteDr = dr;
ftp.SetCurrentDirectory(remoteDr);
ftp.PutFile(localFile, newRemoteFile);
return 1;//上传成功
}
catch (Exception err)
{
return 0;//上传失败
}
finally
{
ftp.Close();
}
}