在试图使用FluentFTP通过FTP上传文件时,我试图捕获拒绝访问的异常。
try
{
client = new FtpClient(serverName, userName, password);
client.AutoConnect();
client.RetryAttempts = 3;
client.UploadFile(localPath, serverPath, FtpRemoteExists.Overwrite, false,FtpVerify.Retry);
}
catch (Exception ex)
{
if (ex is FtpException && ex.InnerException?.Message == "Access is denied. ")
{
//Do something here
throw ex;
}
throw;
}我不能依赖“访问被拒绝”。但是我不知道怎么抓住这个异常。
发布于 2022-04-01 09:35:00
我建议您在继续上传之前检查该目录:
if (!client.DirectoryExists(serverPath))
{
//do somthing...
}您还可以尝试获取文件/目录的权限并捕获它引发的异常:
try
{
...
var ftpListItem = client.GetFilePermissions(pathOnTheServer);
if (ftpListItem.GroupPermissions == FtpPermission.None ||
ftpListItem.OthersPermissions == FtpPermission.None
) //or other permission category..
{
//do something...
return;
}
//do other things..
}
catch (FtpCommandException ex) //get permission failed
{
//handle exception
}
catch(Exception ex)
{
//hendel other exceptions
}弹跳:您可以使用using语句在使用客户端之后正确地释放它:
using (var client = new FtpClient(serverName, userName, password))
{
//your code...
}https://stackoverflow.com/questions/71703978
复制相似问题