Topic "Winscp c# assembly Script: Error deleting file '<Path>' while getfiles"

Author Message
Femil Shajin


This is my script in c# to getfiles and delete from sftp, When I connect through filezilla I can delete files. but when I run the script the log file says permission denied.

C# Script
SessionOptions sessionOptions = new SessionOptions
                    Protocol = Protocol.Sftp,
                    PortNumber = 22,
                    HostName = sftpHost,
                    UserName = sftpUsername,
                    Password = sftpPassword,
                    SshHostKeyFingerprint = sshHostKeyFingerprint,
                    SshPrivateKeyPath = @"E:\Femil\SFTPSample\SFTPSample\CKV022_PVT.ppk",
                    TimeoutInMilliseconds = 90000,

                using (Session session = new Session())
                    session.SessionLogPath = logPath+"\\log.txt";
                    session.Open(sessionOptions); //Attempts to connect to your sFtp site
                    //Get Ftp File
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode - Automatic, Binary, or Ascii
                    transferOptions.FilePermissions = null;  //Permissions applied to remote files;
                    //<em style="font-size: 9pt;">null for default permissions. 
                    //Can set <em style="font-size: 9pt;">user, Group, or other Read/Write/Execute permissions. 
                    transferOptions.PreserveTimestamp = false;  //Set last write time of destination file
                    //to that of source file - basically change the timestamp to match destination and source files.   
                    transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;

                    TransferOperationResult transferResult;
                    //the parameter list is: remote Path, Local Path with filename
                    //(optional - if different from remote path), Delete source file?, transfer Options 
                    transferResult = session.GetFiles("/" + remotePath, localPath, false, transferOptions);
                    //Throw on any error
                    //Log information and break out if necessary 

                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                        Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                        WriteLog("Download of " + transfer.FileName + " succeeded");

Log File
< 2015-08-27 22:03:45.101 Script: Error deleting file '<Path>'.
< 2015-08-27 22:03:45.101 Script: Permission denied.
< 2015-08-27 22:03:45.101 Error code: 3
< 2015-08-27 22:03:45.101 Error message from server (en): Permission denied
. 2015-08-27 22:03:45.101 Script: Failed
> 2015-08-27 22:03:45.180 Script: exit
. 2015-08-27 22:03:45.180 Script: Exit code: 1
. 2015-08-27 22:03:45.180 Closing connection.
. 2015-08-27 22:03:45.180 Sending special code: 12
. 2015-08-27 22:03:45.180 Sent EOF message
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26890
Location: Prague, Czechia
There's no call to deleting files in your code.

Can you attach a full non-obfuscated log?

Can you delete the file in WinSCP GUI?

You can post new topics in this forum


What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License