本文整理汇总了C#中Renci.SshNet.SftpClient.DeleteFile方法的典型用法代码示例。如果您正苦于以下问题:C# SftpClient.DeleteFile方法的具体用法?C# SftpClient.DeleteFile怎么用?C# SftpClient.DeleteFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Renci.SshNet.SftpClient
的用法示例。
在下文中一共展示了SftpClient.DeleteFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Sftp
private bool Sftp(string server, int port, bool passive, string username, string password, string filename, int counter, byte[] contents, out string error, bool rename)
{
bool failed = false;
error = "";
try
{
int i = 0;
filename = filename.Replace("{C}", counter.ToString(CultureInfo.InvariantCulture));
if (rename)
filename += ".tmp";
while (filename.IndexOf("{", StringComparison.Ordinal) != -1 && i < 20)
{
filename = String.Format(CultureInfo.InvariantCulture, filename, Helper.Now);
i++;
}
var methods = new List<AuthenticationMethod> { new PasswordAuthenticationMethod(username, password) };
var con = new ConnectionInfo(server, port, username, methods.ToArray());
using (var client = new SftpClient(con))
{
client.Connect();
var filepath = filename.Trim('/').Split('/');
var path = "";
for (var iDir = 0; iDir < filepath.Length - 1; iDir++)
{
path += filepath[iDir] + "/";
try
{
client.CreateDirectory(path);
}
catch
{
//directory exists
}
}
if (path != "")
{
client.ChangeDirectory(path);
}
filename = filepath[filepath.Length - 1];
using (Stream stream = new MemoryStream(contents))
{
client.UploadFile(stream, filename);
if (rename)
{
try
{
//delete target file?
client.DeleteFile(filename.Substring(0, filename.Length - 4));
}
catch (Exception)
{
}
client.RenameFile(filename, filename.Substring(0, filename.Length - 4));
}
}
client.Disconnect();
}
MainForm.LogMessageToFile("SFTP'd " + filename + " to " + server + " port " + port, "SFTP");
}
catch (Exception ex)
{
error = ex.Message;
failed = true;
}
return !failed;
}