0

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);
captain_majid
  • 140
  • 13

2 Answers2

0

You aren't appending text. Try this.

System.IO.StreamWriter sw;
sw = System.IO.File.AppendText(LogFilePath);
sw.WriteLine("Write Something");
sw.Close();
  • 1
    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: ```c# 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); ``` – captain_majid Nov 02 '21 at 05:53
  • Then I would suggest you to use SFTP protocol if possible. SharpSSH can do SFTP. This might solve your credential visibility issue in WireShark. – Khaled Md Tuhidul Hossain Nov 02 '21 at 06:11
  • SharpSSH is dead project, do not use it. – Martin Prikryl Nov 02 '21 at 07:22
  • Use this instead : https://github.com/sshnet/SSH.NET – Khaled Md Tuhidul Hossain Nov 02 '21 at 08:07
  • @KhaledMdTuhidulHossain My FTP server doesn't allow SFTP (port 22), only FTP/FTPS (port 21) . – captain_majid Nov 02 '21 at 15:53
  • @MartinPrikryl My FTP server doesn't allow SFTP (port 22), only FTP/FTPS (port 21) . – captain_majid Nov 02 '21 at 15:56
0

WinSCP OverwriteMode.Append is supported with SFTP protocol only.


If you want to use encrypted FTPS with FtpWebRequest, just set FtpWebRequest.EnableSsl.
See FTPS (FTP over SSL) in C#.

Also, as you are setting TlsHostCertificateFingerprint in WinSCP, it seems that your server is using self-signed certificate. Then you might need this with FtpWebRequest:
FtpWebRequest "The remote certificate is invalid according to the validation procedure"

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992