我需要将今天是csv文件的文件恢复到本地目录中,我是从流畅的ftp网站使用此示例的,但无法正常运行/ in /目录,并且那里有测试文件但它没有下载文件。
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
答案 0 :(得分:1)
FtpClient.DownloadFile()
的第一个参数是文件名,而不是目录名。如果您提供目录名称,则操作将因该异常而失败,因为您无法在目录上打开FileStream
(这是FluentFTP库在内部执行的操作)。
例如,您可以像这样构建目标文件名:
var localFile = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "Fuel", file.Name);