本文整理汇总了C#中Renci.SshNet.SftpClient.GetLastWriteTime方法的典型用法代码示例。如果您正苦于以下问题:C# SftpClient.GetLastWriteTime方法的具体用法?C# SftpClient.GetLastWriteTime怎么用?C# SftpClient.GetLastWriteTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Renci.SshNet.SftpClient
的用法示例。
在下文中一共展示了SftpClient.GetLastWriteTime方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryGetFileFromFtp
public bool TryGetFileFromFtp(string fileName, DateTime lastUpdated)
{
if (!DirectoryUtil.VerifyDirectory(fileName))
{
return false;
}
var fileToDownload = fileName;
Log.Debug(string.Format("Attempting to download file " + fileToDownload));
try
{
Log.Debug("Opening FTP Connection to " + Hostname);
using (var client = new SftpClient(Hostname, Username, Password))
{
client.Connect();
Log.Debug(string.Format("Connection to {0} opened.", Hostname));
var fileUpdated = client.GetLastWriteTime(fileName);
Log.Debug(string.Format("File {0} was last modified on {1}.", fileName, DateUtil.ToIsoDate(fileUpdated)));
if (fileUpdated <= lastUpdated)
{
Log.Info(string.Format("Did not download file {0}, it was last modified {1} and we last processed it on {2}.",
fileName, DateUtil.ToIsoDate(fileUpdated), DateUtil.ToIsoDate(lastUpdated)), this);
return false;
}
var outputPath = string.Format("{0}\\{1}", UserCsvImportSettings.CsvFolderPath, fileName);
Log.Debug(string.Format("Downloading file {0} and saving to path {1}.", fileName, outputPath));
using (var fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
{
client.DownloadFile(fileName, fileStream);
Log.Debug("File successfully written to " + fileStream.Name);
}
Log.Debug("File Download complete.");
Log.Debug("Updating timestamp.");
File.SetLastWriteTime(outputPath, fileUpdated);
Log.Debug("File timestamp set to " + fileUpdated.ToString());
}
}
catch (Exception e)
{
Log.Error("File did not download successfully.", this);
Log.Error(string.Format("Could not download file {0} from {1} using user {2}.", fileToDownload, Hostname, Username), e,
this);
return false;
}
return true;
}