我需要将今天的 csv 文件恢复到本地目录中下载文件。
public void GetListing()
{
using (FtpClient conn = new FtpClient())
{
conn.Host = ftpIpAddress;
conn.Credentials = new NetworkCredential(ftpUserName,ftpPassword);
foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
FtpListOption.Modify | FtpListOption.Size))
{
switch (item.Type)
{
case FtpFileSystemObjectType.Directory:
break;
case FtpFileSystemObjectType.File:
Console.Write("Filename " + item.FullName);
conn.DownloadFile(item.FullName,"/in/");
break;
case FtpFileSystemObjectType.Link:
// derefernece symbolic links
if (item.LinkTarget != null)
{
// see the DereferenceLink() example
// for more details about resolving links.
item.LinkObject = conn.DereferenceLink(item);
if (item.LinkObject != null)
{
// switch (item.LinkObject.Type)...
}
}
break;
}
}
// same example except automatically dereference symbolic links.
// see the DereferenceLink() example for more details about resolving links.
foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
FtpListOption.Modify | FtpListOption.Size | FtpListOption.DerefLinks))
{
switch (item.Type)
{
case FtpFileSystemObjectType.Directory:
break;
case FtpFileSystemObjectType.File:
Console.Write("File " + item.FullName);
break;
case FtpFileSystemObjectType.Link:
if (item.LinkObject != null)
{
// switch (item.LinkObject.Type)...
}
break;
}
}
}
}
我已经在 127.0.1 上使用 filezilla 服务器设置了一个测试环境,我正在将我的详细信息传递给下面的班级
ServerConnection connection = new ServerConnection();
connection.ftpIpAddress = "127.0.0.1";
connection.ftpUserName = "ftpuser";
connection.ftpPassword = "ftppassword";
connection.LocalDestDirectory = @"C:\ImportedFromPump\in";
connection.remoteDirectory = @"\in\";
我想将文件存储在本地目录值中,并且当它们被下载时,我希望从 ftp 中删除它们,但我不确定如何去做。
我一直在这里关注本教程。
https://github.com/robinrodricks/FluentFTP/blob/master/FluentFTP.Examples/GetListing.cs
调试信息的结果:
# OpenPassiveDataStream(AutoPassive, "MLSD /", 0)
Command: EPSV
Response: 229 Entering Extended Passive Mode (|||63478|)
Status: Connecting to 127.0.0.1:63478
Command: MLSD /
Response: 150 Opening data channel for directory listing of "/"
+---------------------------------------+
Listing: type=dir;modify=20181018113309; Archived
Listing: type=dir;modify=20181018115328; in
-----------------------------------------
Status: Disposing FtpSocketStream...
# CloseDataStream()
Response: 226 Successfully transferred "/"
Status: Disposing FtpSocketStream...
# Dispose()
Status: Disposing FtpClient object...
Command: QUIT
Response: 221 Goodbye
Status: Disposing FtpSocketStream...
Status: Disposing FtpSocketStream...
编辑 2
好的,所以我更进一步并更改了我的代码以使用下载文件,但现在我收到一条访问被拒绝的消息。
public void GetListing()
{
try
{
using (FtpClient conn = new FtpClient())
{
IEnumerable<string> directorys = new[] { "/in" };
conn.Host = ftpIpAddress;
conn.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
FtpListItem[] files = conn.GetListing("/in/", FtpListOption.AllFiles)
.Where(x => x.Type == FtpFileSystemObjectType.File)
.OrderBy(x => x.Modified)
.ToArray();
foreach(FtpListItem file in files)
{
conn.DownloadFile(Environment.GetEnvironmentVariable("LocalAppData") + @"\Fuel\",file.FullName, true);
}
}
}
catch
(Exception ex)
{
}
}
错误在这里
DownloadFile("C:\Users\user\AppData\Local\Fuel\", "/in/FuelPumpData.csv", True, None) 抛出异常:
mscorlib.dll 中的“System.IO.DirectoryNotFoundException”
甚至认为它是一个用户文件夹,它确实存在。
编辑 3 以显示 ftp 目录存在
编辑 4
证明 .net 对象正在代码中查找文件。
编辑 6