本文整理汇总了C#中Renci.SshNet.SftpClient.Delete方法的典型用法代码示例。如果您正苦于以下问题:C# SftpClient.Delete方法的具体用法?C# SftpClient.Delete怎么用?C# SftpClient.Delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Renci.SshNet.SftpClient
的用法示例。
在下文中一共展示了SftpClient.Delete方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadFiles
/// <summary>
/// Uploads the files.
/// </summary>
/// <param name="fileList">The file list.</param>
/// <exception cref="System.Exception">Remote File Already Exists.</exception>
public void UploadFiles(List<ISFTPFileInfo> fileList)
{
try
{
this.Log(String.Format("Connecting to Host: [{0}].", this.hostName), LogLevel.Minimal);
using (SftpClient sftp = new SftpClient(this.hostName, this.portNumber, this.userName, this.passWord))
{
sftp.Connect();
this.Log(String.Format("Connected to Host: [{0}].", this.hostName), LogLevel.Verbose);
// Upload each file
foreach (SFTPFileInfo sftpFile in fileList)
{
FileInfo fileInfo = new FileInfo(sftpFile.LocalPath);
if (sftpFile.RemotePath.EndsWith("/"))
sftpFile.RemotePath = Path.Combine(sftpFile.RemotePath, fileInfo.Name).Replace(@"\", "/");
// if file exists can we overwrite it.
if (sftp.Exists(sftpFile.RemotePath))
{
if (sftpFile.OverwriteDestination)
{
this.Log(String.Format("Removing File: [{0}].", sftpFile.RemotePath), LogLevel.Verbose);
sftp.Delete(sftpFile.RemotePath);
this.Log(String.Format("Removed File: [{0}].", sftpFile.RemotePath), LogLevel.Verbose);
}
else
{
if (this.stopOnFailure)
throw new Exception("Remote File Already Exists.");
}
}
using (FileStream file = File.OpenRead(sftpFile.LocalPath))
{
this.Log(String.Format("Uploading File: [{0}] -> [{1}].", fileInfo.FullName, sftpFile.RemotePath), LogLevel.Minimal);
sftp.UploadFile(file, sftpFile.RemotePath);
this.Log(String.Format("Uploaded File: [{0}] -> [{1}].", fileInfo.FullName, sftpFile.RemotePath), LogLevel.Verbose);
}
}
}
this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal);
}
catch (Exception ex)
{
this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal);
this.ThrowException("Unable to Upload: ", ex);
}
}
示例2: DownloadFiles
/// <summary>
/// Downloads the files.
/// </summary>
/// <param name="fileList">The file list.</param>
/// <param name="failRemoteNotExists">if set to <c>true</c> [fail remote not exists].</param>
/// <exception cref="System.Exception">
/// Local File Already Exists.
/// </exception>
public void DownloadFiles(List<ISFTPFileInfo> fileList)
{
try
{
this.Log(String.Format("Connecting to Host: [{0}].", this.hostName), LogLevel.Minimal);
using (SftpClient sftp = new SftpClient(this.hostName, this.portNumber, this.userName, this.passWord))
{
sftp.Connect();
this.Log(String.Format("Connected to Host: [{0}].", this.hostName), LogLevel.Verbose);
// Download each file
foreach (SFTPFileInfo filePath in fileList)
{
if (!sftp.Exists(filePath.RemotePath))
{
this.Log(String.Format("Remote Path Does Not Exist: [{0}].", this.hostName), LogLevel.Verbose);
if (this.stopOnFailure)
throw new Exception(String.Format("Remote Path Does Not Exist: [{0}]", filePath.RemotePath));
else
continue;
}
if (Directory.Exists(filePath.LocalPath))
filePath.LocalPath = Path.Combine(filePath.LocalPath, filePath.RemotePath.Substring(filePath.RemotePath.LastIndexOf("/") + 1));
// Can we overwrite the local file
if (!filePath.OverwriteDestination && File.Exists(filePath.LocalPath))
throw new Exception("Local File Already Exists.");
this.Log(String.Format("Downloading File: [{0}] -> [{1}].", filePath.RemotePath, filePath.LocalPath), LogLevel.Minimal);
using (FileStream fileStream = File.OpenWrite(filePath.LocalPath))
{
sftp.DownloadFile(filePath.RemotePath, fileStream);
}
this.Log(String.Format("File Downloaded: [{0}]", filePath.LocalPath), LogLevel.Verbose);
// Remove remote file
if (filePath.RemoveSource)
{
this.Log(String.Format("Removing File: [{0}]", filePath.RemotePath), LogLevel.Minimal);
sftp.Delete(filePath.RemotePath);
this.Log(String.Format("Removed File: [{0}]", filePath.RemotePath), LogLevel.Verbose);
}
}
}
this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal);
}
catch (Exception ex)
{
this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal);
this.ThrowException("Unable to Download: ", ex);
}
}
示例3: DeleteRemoteFile
public static void DeleteRemoteFile(string host, string file, string username, string password)
{
using (var sftp = new SftpClient(host, username, password))
{
sftp.Connect();
sftp.Delete(file);
sftp.Disconnect();
}
}