如何从代码连接ftp
本文关键字:连接 ftp 代码 | 更新日期: 2023-09-27 18:27:54
你好,在这段代码中,我如何将ftp替换为驱动程序地址上的服务器?
string[] filePaths = Directory.GetFiles(@"c:'MyDir'", "*.bmp");
例如:
string[] filePaths = Directory.GetFiles(@"our ftp address", "*.bmp");
第一个代码运行良好,但第二个代码不起作用!?tnx
示例http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspxDisplayFileFromServer(您的bmp文件)
`public static bool DisplayFileFromServer(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
try
{
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return true;
}`
这个短程序将使用ListDirectoryDetails方法列出FTP服务器中的文件和目录。还有一个ListDirectory方法,它只列出名称。
using System;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebRequest ftp = FtpWebRequest.Create("ftp://ftp.ed.ac.uk/");
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
using (WebResponse rsp = ftp.GetResponse())
{
using (StreamReader reader = new StreamReader(rsp.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
}
}