C# Fluent ftp获取最新文件并下载
我需要把今天的文件(csv文件)带回本地目录,我使用的是fluent ftp网站上的这个例子,但它不起作用它找到了/in/目录ok,那里有一个测试文件,但它没有下载文件C# Fluent ftp获取最新文件并下载,c#,fluentftp,C#,Fluentftp,我需要把今天的文件(csv文件)带回本地目录,我使用的是fluent ftp网站上的这个例子,但它不起作用它找到了/in/目录ok,那里有一个测试文件,但它没有下载文件 public void GetListing() { using (FtpClient conn = new FtpClient()) { conn.Host = ftpIpAddress; conn.Credentials = new Netwo
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中删除,但我不确定如何执行此操作
我一直在遵循这个教程在这里
调试信息的结果:
# 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)
{
}
}
public void GetListing()
{
尝试
{
使用(FtpClient conn=new FtpClient())
{
IEnumerable directorys=new[]{”/in“};
连接主机=ftpIpAddress;
连接凭据=新网络凭据(ftpUserName、ftpPassword);
FtpListItem[]files=conn.GetListing(“/in/”,ftplistotion.AllFiles)
.Where(x=>x.Type==FtpFileSystemObjectType.File)
.OrderBy(x=>x.Modified)
.ToArray();
foreach(文件中的FtpListItem文件)
{
conn.DownloadFile(Environment.GetEnvironmentVariable(“LocalAppData”)+@“\Fuel\”,file.FullName,true);
}
}
}
抓住
(例外情况除外)
{
}
}
错误就在这里
下载文件(“C:\Users\user\AppData\Local\Fuel\”,“/in/FuelPumpData.csv”,True,None)引发异常:
mscorlib.dll中的“System.IO.DirectoryNotFoundException”
甚至认为它是一个用户文件夹,它存在到底是怎么回事
编辑2
以证明该目录存在。
编辑3
以显示ftp目录存在
编辑4
证明.net对象正在代码中查找文件
编辑5
以显示目录权限。
编辑6
FtpClient.DownloadFile()的第一个参数是文件名,而不是目录名。如果提供目录名,操作将失败,出现该异常,因为您无法在目录上打开
FileStream
(这是FluentFTP库在内部执行的操作)
例如,您可以按如下方式构建目标文件名:
var localFile = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "Fuel", file.Name);
目录
Fuel\in
不存在。它存在,它是他们的,它不是Fuel\in,它只是Fuel。请仔细阅读。我想CodeCaster的意思是“in”目录不存在。.csv的路径以名为“in”的目录开始。问题不在FTP服务器中。尝试写入文件时,这是.NET的一个例外。您不应该提供一个文件名作为DownloadFile()
的第一个参数,而不是目录名吗?其中一个要在当天晚些时候提供:-(