尝试将文件上传到本地的SFTP服务器。连接SFTP时发生错误。
我尝试使用命令行和FileZilla进行连接。它可以连接。
public static void UploadToSFTP(List<string> selectedFileList)
{
using (var connection = new SQLiteConnection(GeneralFunc.DBFile))
{
List<Model.SFTPSetting> sftpList = new List<Model.SFTPSetting>();
string host = "myiphere";
int port = 22;
string username = "gth";
string password = "1234";
string dir = @"/D/SFTP";
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
client.ChangeDirectory(dir);
foreach (var file in selectedFileList)
{
using (FileStream fs = new FileStream(testPath + "/" + file, FileMode.Open))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, Path.GetFileName(testPath + "/" + file));
}
}
}
}
}
未处理的异常:
<!-- language: lang-none -->
System.ObjectDisposedException: Safe handle has been closed
01-09 16:28:12.248 E/mono (18955):
01-09 16:28:12.248 E/mono (18955): Unhandled Exception:
01-09 16:28:12.248 E/mono (18955): System.ObjectDisposedException: Safe handle has been closed
01-09 16:28:12.248 E/mono (18955): at System.Runtime.InteropServices.SafeHandle.DangerousAddRef (System.Boolean& success) [0x0001c] in <43dbbdc147f2482093d8409abb04c233>:0
01-09 16:28:12.248 E/mono (18955): at System.Threading.NativeEventCalls.SetEvent (Microsoft.Win32.SafeHandles.SafeWaitHandle handle) [0x00002] in <43dbbdc147f2482093d8409abb04c233>:0
01-09 16:28:12.248 E/mono (18955): at System.Threading.EventWaitHandle.Set () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0
01-09 16:28:12.248 E/mono (18955): at (wrapper remoting-invoke-with-check) System.Threading.EventWaitHandle.Set()
未处理的异常:
<!-- language: lang-none -->
System.Net.Sockets.SocketException: The socket is not connected
01-09 16:28:17.309 E/mono-rt (18955): [ERROR] FATAL UNHANDLED EXCEPTION: System.ObjectDisposedException: Safe handle has been closed
01-09 16:28:17.309 E/mono-rt (18955): at System.Runtime.InteropServices.SafeHandle.DangerousAddRef (System.Boolean& success) [0x0001c] in <43dbbdc147f2482093d8409abb04c233>:0
01-09 16:28:17.309 E/mono-rt (18955): at System.Threading.NativeEventCalls.SetEvent (Microsoft.Win32.SafeHandles.SafeWaitHandle handle) [0x00002] in <43dbbdc147f2482093d8409abb04c233>:0
01-09 16:28:17.309 E/mono-rt (18955): at System.Threading.EventWaitHandle.Set () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0