2

I'm trying to upload file using following code but getting error below.

Some notes: I'm using Windows 7. Using CrushFTP SFTP server, able to connect with using FileZilla and WinSCP client, but through code its being nightmare.

Error/exception:

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\Users\xxxxxxx\AppData\Local\Temp\wscp0D64.036B20B7.tmp' because it is being used by another process.

My code to connect is below

SessionOptions sessionOptions = new SessionOptions 
{ 
    Protocol = Protocol.Sftp, 
    HostName = "127.0.0.1", //hostname e.g. IP: 192.54.23.32, or mysftpsite.com 
    UserName = "xxxxxx", 
    Password = "yyyyyy", 
    PortNumber = zzzzz, //some number 
    SshHostKeyFingerprint = "ssh-rsa 1024 ::::04:85:3b:7a::::::::" 
}; 

using (Session session = new Session()) 
{ 
    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; 
    //null for default permissions. Can set 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: local Path, Remote Path, Delete source file?, transfer Options 
    transferResult = session.PutFiles(@"C:\Adnan\a.txt", "/", false, transferOptions); 
    //Throw on any error 
    transferResult.Check(); 
    //Log information and break out if necessary 
}
4

1 回答 1

9

我也遇到了这个异常。对我来说,它是在调用session.Open(...).

但是,这是由 WinSCP 程序集生成和捕获的内部异常。我之所以注意到它,是因为我已将 Visual Studio 配置为在每次抛出异常时停止。如果我关闭此设置(或继续跳过此设置和一些额外的内部 IOExceptions),SFTP 连接将正确打开。

于 2015-04-28T18:34:01.810 回答