2017-02-01 7 views
1

私は問題なしでリモートからローカルにファイルを転送するためにTamir SharpSSHを使用しています。ワイルドカードと一致するすべてのファイルをSFTPにアップロード

しかし、SFTPを介して複数のXMLファイルをアップロードしようとしているが、私はエラーが発生しますとき:

Illegal characters in path.

私はそれが何の問題もなくファイルを転送し、正確なファイル名を使用してアップロードしようとします。

私は2つのXMLファイルをアップロードしようとするたび:

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(); 
+0

'E:\ Zatpark \ *。xml'は私にとって有効なパスではありません。あなたのSftp実装は、このような転送を明示的に許可していますか? (もしそうなら驚くだろう)。 '*'は有効なパス文字ではないため、これは* "不正な文字列" *エラーの原因と考えられます。側近では、この短いコードでも3つの命名規則を使用していても、実際には単一の命名規則を決定する必要があります。 –

+0

パスにワイルドカードマスクを使用し、別のサードパーティのライブラリを使用する場合は、オプションである可能性があります。このhttp://www.componentpro.com/sftp.net/をチェックアウトしたい場合があります。次に例を示します。 http://www.componentpro.com/doc/sftp/upload-files.htm –

答えて

3

はTamir.SharpSSHを使用しないでください、それは死んでプロジェクトです。 up to date SSH/SFTP implementationを使用してください。


あなたはワイルドカードをサポートしていませんSSH.NETのようなライブラリに切り替える場合は、アップロードするファイルを検索するDirectory.GetFiles methodを使用する必要があります。

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)); 
    } 
} 

またはサポートワイルドカードを行うライブラリを使用。 WinSCP .NET assemblyと例えば

(ただし、純粋な.NETアセンブリものではありません)、あなたが行うことができます:

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(); 
} 

あなたはcode template generated in WinSCP GUIを持つことができます。

(私はWinSCPのの著者です)


説明するために、なぜあなたのコードは動作しません:ChannelSftp.glob_local方法を確認してください。その実装は壊れていないと変です。基本的には、*?の完全なマスクのみがサポートされています。

+0

Martinさん、ありがとう、これが私の問題を解決しました。 –

関連する問題