1

I am using Tamir SharpSSH for transferring files from remote to local and vice versa with no issues.

But, when trying to upload multiple XML files via SFTP but I am receiving an error:

Illegal characters in path.

If I try to upload using the exact file name it transfers the file without any issues.

Every time I try to upload two XML files:

KDO_E2D_A21_AA769_20170124_143123.xml
KDO_E2D_A21_AA776_20170130_143010.xml
string ftpURL = "11.11.11.1";
string userName = "Aaaaaa"; //User Name of the SFTP server
string password = "hah4444"; //Password of the SFTP server
int port = 22; //Port No of the SFTP server (if any)

//The directory in SFTP server where the files will be uploaded
string ftpDirectory = "/home/A21sftp/kadoe/";

//Local directory from where the files will be uploaded 
string localDirectory = "E:\\Zatpark\\*.xml"; 

Sftp Connection = new Sftp(ftpURL, userName, password);
Connection.Connect(port);
Connection.Put(localDirectory, ftpDirectory); 
Connection.Close();
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
Kiran V
  • 59
  • 4
  • 16
  • `E:\Zatpark\*.xml` doesn't look like a valid path to me. Does your Sftp implementation explicitly allow a transfer like this? (I'd be surprised if it does). This is likely the cause of the *"illegal characters in path"* error, since `*` is not a valid path character. On a side nide: you should really decide on a single naming convention, even in this short code you are using three different ones. – Manfred Radlwimmer Feb 01 '17 at 06:55
  • If you want to use wildcard masks in the path and using another third-party library might be an option, you may want to checkout this http://www.componentpro.com/sftp.net/, and here is the example: http://www.componentpro.com/doc/sftp/upload-files.htm – faheem khan Apr 26 '17 at 03:32

1 Answers1

4

Do not use Tamir.SharpSSH, it's a dead project. Use some up to date SSH/SFTP implementation.


If you switch to library like SSH.NET that does not support wildcards, you have to use the Directory.GetFiles method to find files to upload:

SftpClient client = new SftpClient("example.com", "username", "password");
client.Connect();

string localDirectory = @"E:\Zatpark upload";
string localPattern = "*.xml";
string ftpDirectory = "/dv/inbound/";
string[] files = Directory.GetFiles(localDirectory, localPattern);
foreach (string file in files)
{
    using (Stream inputStream = new FileStream(file, FileMode.Open))
    {
        client.UploadFile(inputStream, ftpDirectory + Path.GetFileName(file));
    }
}

Or use a library that does support wildcards.

For example with WinSCP .NET assembly (what is not a pure .NET assembly though), you can do:

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "example.com",
    UserName = "username",
    Password = "password",
    SshHostKeyFingerprint = "ssh-dss ...",
};

using (Session session = new Session())
{
    session.Open(sessionOptions);
    string ftpDirectory = "/dv/inbound/"; 
    string localDirectory = @"E:\Zatpark upload\*.xml";
    session.PutFiles(localDirectory, ftpDirectory).Check();
}

You can have a code template generated in WinSCP GUI.

(I'm the author of WinSCP)


To explain, why your code does not work: Check the ChannelSftp.glob_local method. Its implementation is strange, if not broken. It basically supports only masks consisting completely of * and ?'s.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846