本文整理汇总了C#中System.Net.FtpWebResponse类的典型用法代码示例。如果您正苦于以下问题:C# FtpWebResponse类的具体用法?C# FtpWebResponse怎么用?C# FtpWebResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FtpWebResponse类属于System.Net命名空间,在下文中一共展示了FtpWebResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadFtp
/// <summary>
/// 连接FTP的方法
/// </summary>
/// <param name="ftpuri">ftp服务器地址,端口</param>
/// <param name="ftpUserID">用户名</param>
/// <param name="ftpPassword">密码</param>
public DownloadFtp(string ftpuri, string ftpUserID, string ftpPassword)
{
// 根据uri创建FtpWebRequest对象
ft = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpuri));
// ftp用户名和密码
ft.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ft.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
fr = (FtpWebResponse)ft.GetResponse();
stream = fr.GetResponseStream();
////二进制文件读入
//if (!fr.ContentType.ToLower().StartsWith("text/"))
//{
// SaveBinaryFile(fr);
//}
////文本文件
//else
//{
string buffer = "", line;
StreamReader reader = new StreamReader(stream);
while ((line = reader.ReadLine()) != null)
{
buffer += line + "\r\n";
}
//装入整个文件之后,接着就要把它保存为文本文件。
SaveTextFile(buffer);
//}
}
示例2: DownloadFile
public void DownloadFile(string remoteFile, string localFile)
{
// Создаем FTP Request
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(String.Format("{0}/{1}", host, remoteFile));
// Инициализируем сетевые учетные данные
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
// Задаем команду, которая будет отправлена на FTP-сервер
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Timeout = TIMEOUT;
// Ответ FTP-сервера
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
// Возвращаем поток данных
ftpStream = ftpResponse.GetResponseStream();
// Создаем локальный файл
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
// Пишем в файл
ftpStream.CopyTo(localFileStream);
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
示例3: VerificaSeExiste
public bool VerificaSeExiste(string destinfilepath, string ftphost, string ftpfilepath, string user, string pass)
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftphost + "//" + ftpfilepath);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
try {
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
return true;
}
catch (Exception exc)
{
this.sErro = exc.Message;
return false;
}
}
示例4: createDirectory
/* Create a New Directory on the FTP Server */
public bool createDirectory(string newDirectory)
{
bool result = true;
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex)
{
StockLog.Write(ex.ToString());
result = false;
}
return result;
}
示例5: copyToServer
public bool copyToServer(string remote, string local, string logPath)
{
bool success = false;
// try
// {
ftpRequest = (FtpWebRequest)WebRequest.Create(host + remote);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Credentials = netCreds;
byte[] b = File.ReadAllBytes(local);
ftpRequest.ContentLength = b.Length;
using (Stream s = ftpRequest.GetRequestStream())
{
s.Write(b, 0, b.Length);
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
// check was successful
if (ftpResponse.StatusCode == FtpStatusCode.ClosingData)
{
success = true;
logResult(remote, host, user, logPath);
}
ftpResponse.Close();
/* }
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}*/
return success;
}
示例6: ListDirectory
public string[] ListDirectory(string directory)
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(String.Format("{0}/{1}", host, directory));
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpRequest.Timeout = TIMEOUT;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string directoryRaw = null;
try
{
while (ftpReader.Peek() != -1)
{
//TODO: выдергиваем имя файла, доделать...
directoryRaw += ftpReader.ReadLine().Split(' ').Last() + "|";
}
}
catch
{
}
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
try
{
string[] directoryList = directoryRaw.Split("|".ToCharArray());
return directoryList;
}
catch
{
}
return new string[] { };
}
示例7: WriteResponseToFile
private void WriteResponseToFile(string destPath, FtpWebResponse response, Stream responseStream)
{
FileStream writer = new FileStream(destPath, FileMode.Create);
long length = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = responseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
writer.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, bufferSize);
}
writer.Close();
}
示例8: SaveBinaryFile
//二进制文件存储方法
protected void SaveBinaryFile(FtpWebResponse response)
{
byte[] buffer = new byte[1024];
string filename = convertFilename(response.ResponseUri);
Stream outStream = File.Create(filename);
Stream inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0,
buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
} while (l > 0);
outStream.Close();
}
示例9: createDirectory
/* Create a New Directory on the FTP Server */
public static void createDirectory(Data.RemoteServer data, string newDirectory)
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(data.adress + newDirectory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(data.login, data.password);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
示例10: delete
/* Delete File */
public void delete(string deleteFile)
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
示例11: Download
/// <summary>
/// 下載FTP檔案
/// 存儲至Client端桌面
/// </summary>
/// <param name="FileName">要下載的檔案名稱</param>
/// <returns></returns>
public static bool Download(string FileName)
{
bool result = false;
string LocalFile = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + FileName;
try
{
Uri FtpPath = new Uri(uriServer + FileName);
ftpRequest = (FtpWebRequest)WebRequest.Create(FtpPath);
ftpRequest.Credentials = new NetworkCredential(UserName,Password);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
ftpFileStream = new FileStream(LocalFile, FileMode.Create);
int BufferSize = 2048;
byte[] byteBuffer = new byte[BufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, BufferSize);
try
{
while (bytesRead > 0)
{
ftpFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, BufferSize);
}
result = true;
}
catch (Exception ex)
{
sysMessage.SystemEx(ex.Message);
}
ftpFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex)
{
sysMessage.SystemEx(ex.Message);
}
return result;
}
示例12: download
/* Download File */
public void download(string remoteFile, string localFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Get the FTP Server's Response Stream */
ftpStream = ftpResponse.GetResponseStream();
/* Open a File Stream to Write the Downloaded File */
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Download the File by Writing the Buffered Data Until the Transfer is Complete */
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
示例13: Delete
/* Delete File */
public void Delete(string deleteFile)
{
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
示例14: EliminarArchivo
public void EliminarArchivo(string ArchivoEliminar)
{
try
{
Console.WriteLine("Iniciando proceso de eliminacion de archivo iniciado.");
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(Direccion + "/" + ArchivoEliminar);
ftpRequest.Credentials = new NetworkCredential(Usuario, Password);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Console.WriteLine("Archivo eliminado correctamente.");
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception)
{
Console.WriteLine("No se encuentra el archivo a eliminar");
}
}
示例15: DownloadFile
/// <summary>
/// 下载文件
/// </summary>
/// <param name="remoteFile"></param>
/// <returns></returns>
public byte[] DownloadFile(string remoteFile)
{
this.Connect("ftp://" + this._serverIP + "/" + remoteFile);
this.ftpRequest.Method = "RETR";
using (this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse())
{
using (this.ftpStream = this.ftpResponse.GetResponseStream())
{
byte[] byteBuffer = new byte[this.bufferSize];
using (MemoryStream memoryStream = new MemoryStream())
{
for (int bytesRead = this.ftpStream.Read(byteBuffer, 0, this.bufferSize); bytesRead > 0; bytesRead = this.ftpStream.Read(byteBuffer, 0, this.bufferSize))
{
memoryStream.Write(byteBuffer, 0, bytesRead);
}
return memoryStream.Length > 0 ? memoryStream.ToArray() : null;
}
}
}
}