I'm trying to add text to a text file in a FTP server, but it overwrites it instead of appending, how to solve this ?
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "www.site.com",
UserName = "name",
Password = "pass",
FtpSecure = FtpSecure.Explicit,
TlsHostCertificateFingerprint = "what ever that is",
};
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions options = new TransferOptions();
options.TransferMode = TransferMode.Ascii;
options.OverwriteMode = OverwriteMode.Append;
TransferOperationResult transferResult;
transferResult = session.PutFiles(@"E:\Up\log.txt", "/log.txt", false, options);
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
Found something similar here, but no solution: https://winscp.net/forum/viewtopic.php?t=25031
I don't really want to download the file, append to it, then upload it, below code worked but my credentials are visible to WireShark:
byte[] data = Encoding.UTF8.GetBytes(@"Hello World.");
Uri target = new Uri("ftp://site.com/test.txt");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.AppendFile;
request.ContentLength = data.Length;
request.Credentials = new NetworkCredential("name", "pass");
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(data, 0, data.Length);