I am using EDTFTPNETPRO 4.0 and communicating with the FTP server using SFTP. I get this error very sporadically:
"Exception has been thrown by the target of an invocation." Attached is the code from my application for FTP'ing documents
private void sftpConnect(string strIPAddress,string strLogin, string strPassword, int iPort)
{
//declare the events for the ftp component
utils.sftpConnection.Connected+=new EnterpriseDT.Net.Ftp.FTPConnectionEventHandler(sftpConnection_Connected);
// setting server address and credentials
utils.sftpConnection.ServerAddress = strIPAddress;
utils.sftpConnection.ServerPort = iPort;
utils.sftpConnection.UserName = strLogin;
utils.sftpConnection.Password = strPassword;
// select SFTP
utils.sftpConnection.Protocol = FileTransferProtocol.SFTP;
// turn off server validation
utils.sftpConnection.ServerValidation = SecureFTPServerValidationType.None;
// connect to server
IAsyncResult result = utils.sftpConnection.BeginConnect(null,null);
while(!result.IsCompleted)
{
Application.DoEvents();
}
utils.sftpConnection.EndConnect(result);
//change the current directory to AllPdfDocs
try
{
utils.sftpConnection.ChangeWorkingDirectory("AllPdfDocs");
}
catch
{
result = utils.sftpConnection.BeginCreateDirectory("AllPdfDocs",null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndCreateDirectory(result);
result = utils.sftpConnection.BeginChangeWorkingDirectory("AllPdfDocs",null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndChangeWorkingDirectory(result);
}
//change the current directory to DMS
try
{
utils.sftpConnection.ChangeWorkingDirectory("DMS");
}
catch
{
result = utils.sftpConnection.BeginCreateDirectory("DMS",null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndCreateDirectory(result);
result = utils.sftpConnection.BeginChangeWorkingDirectory("DMS",null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndChangeWorkingDirectory(result);
}
//change current directory to physid
try
{
utils.sftpConnection.ChangeWorkingDirectory(utils.phys_name);
}
catch
{
result = utils.sftpConnection.BeginCreateDirectory(utils.phys_name,null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndCreateDirectory(result);
result = utils.sftpConnection.BeginChangeWorkingDirectory(utils.phys_name,null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndChangeWorkingDirectory(result);
}
//create a new directory with name as date time and make it the current directory
repDate = replaceDate();
remoteFolderPath = @"/AllPdfDocs/DMS/" + utils.phys_name + @"/"+ repDate;
result = utils.sftpConnection.BeginCreateDirectory(repDate,null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndCreateDirectory(result);
result = utils.sftpConnection.BeginChangeWorkingDirectory(repDate,null,null);
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndChangeWorkingDirectory(result);
//get the list of pdf files to be uploaded
string[] fileEntries = Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory + @"\updatefolder","*.pdf");
for(int i = fileEntries.GetLowerBound(0); i <= fileEntries.GetUpperBound(0); i++)
{
//connect to the ftp server if the connection has been disconnected
if(!utils.sftpConnection.IsConnected)
{
utils.sftpConnection.BeginConnect( null, null );
while( !result.IsCompleted )
Application.DoEvents();
utils.sftpConnection.EndConnect( result );
}
//get the file name excluding the path
strTransferedFile = fileEntries[i].ToString().Substring(fileEntries[i].ToString().LastIndexOf(@"\")+1);
//upload the file to the ftp server
utils.sftpConnection.UploadFile(fileEntries[i],strTransferedFile);
//get the files in current remote directory
string[] strArrRemoteFiles = utils.sftpConnection.GetFiles();
for(int iFileCount = strArrRemoteFiles.GetLowerBound(0); iFileCount <= strArrRemoteFiles.GetUpperBound(0); iFileCount++)
{
//if the file is found then call the function to update the sql server
if(strArrRemoteFiles[iFileCount].Trim() == strTransferedFile)
{
listBox1.Items.Add( "<< File uploaded. Data updation pending");
insertDocsToSql(strTransferedFile);
}
}
}
//close the connection
result = utils.sftpConnection.BeginClose( null, null );
while( !result.IsCompleted )
{
Application.DoEvents();
}
utils.sftpConnection.EndClose( result );
utils.sftpConnection.Connected-=new EnterpriseDT.Net.Ftp.FTPConnectionEventHandler(sftpConnection_Connected);
}