yes i have 4 instances of the session class
I have also noted the FTP.cs which is instantiated through various clients calling them and also the debug log for the first three
You will see the error in 2nd and 3rd FTP.
I read on the release notes that in version 5.14 some problem of this type might be solved. Is that correct? Should i install the new version?
I am already using the new DLL but the installation is of an old version
Thanks
public class FTP
{
Session _session = new Session();
SessionOptions _options;
TransferOptions _transferOptions;
public string _remoteDirectory = string.Empty;
public FTP(string _hostName, string _password, int _portNumber, string _userName, string _debugPath, string _sessionPath)
{
_session.DebugLogPath = String.Format(_debugPath, "WINSCP_Level3_Debuglog" + DateTime.Now.ToString("MMddyyyyHHmmss"));
_session.SessionLogPath = String.Format(_sessionPath, "WINSCP_Level3_Sessionlog" + DateTime.Now.ToString("MMddyyyyHHmmss"));
_session.DisableVersionCheck = true;
_session.DefaultConfiguration = false;
_session.ExecutablePath = ConfigurationManager.AppSettings["ExecutableProperty"];
_session.Timeout = System.TimeSpan.FromMilliseconds(60000);
try
{
_session.Open(Initialize(_hostName, _password, _portNumber, _userName));
}
catch (Exception ex)
{
throw ex;
}
}
private SessionOptions Initialize(string _hostName, string _password, int _portNumber, string _userName)
{
_options = new SessionOptions();
_options.Protocol = Protocol.Ftp;
_options.Password = _password;
_options.HostName = _hostName;
_options.PortNumber = _portNumber;
_options.UserName = _userName;
return _options;
}
private TransferOptions SetTOptions()
{
_transferOptions = new TransferOptions();
_transferOptions.TransferMode = TransferMode.Automatic;
//_transferOptions.PreserveTimestamp = true;
return _transferOptions;
}
public bool UploadFile(string temporary_path, string _filename, bool directory2Upload)
{
TransferOperationResult transferResult;
string dirPath2UploadFrom = String.Empty;
string dirPath2Upload2 = String.Empty;
string temporaryFileName = String.Empty;
string dirPath2Move = String.Empty;
string filePath = String.Empty;
bool replaceFileOnTransfer = false;
FileInfo uploadfile;
FileInfo temporary_pathDir;
Random r = new Random();
try
{
temporary_pathDir = new FileInfo(temporary_path);
temporaryFileName = temporary_pathDir.DirectoryName + @"\" + r.Next(100).ToString();
File.Copy(temporary_path, temporaryFileName);
Utils.LogFile("New Temporary File: " + temporaryFileName);
temporary_path = temporaryFileName;
uploadfile = new FileInfo(_filename);
replaceFileOnTransfer = Boolean.Parse(ConfigurationManager.AppSettings["ReplaceFilesOnTransfer"]);
if (!_remoteDirectory.Equals(String.Empty))
filePath = _remoteDirectory + @"/" + uploadfile.Directory.Name + @"/" + Path.GetFileName(_filename);
else
filePath = uploadfile.Directory.Name + @"/" + Path.GetFileName(_filename);
Utils.LogFile("[Info] [UploadFile]: Local Path: " + temporary_path);
Utils.LogFile("[Info] [UploadFile]: Remote Path: " + filePath);
if (replaceFileOnTransfer)
{
transferResult = _session.PutFiles(temporary_path, filePath, true, SetTOptions());
Utils.LogFile("[Info] [DeleteFile]: Deleted File Name: " + filePath + " as replaceFileOnTransfer is True");
transferResult.Check();
}
else
{
if (_session.FileExists(filePath))
{
Utils.LogFile("[Info] [UploadFile]: Movie Name " + Path.GetFileName(_filename) + " already exists on the remote location and has not been transferred");
return true;
}
else
{
transferResult = _session.PutFiles(temporary_path, filePath, false, SetTOptions());
transferResult.Check();
}
}
Utils.LogFile("[Info] [UploadFile]: Movie Name " + Path.GetFileName(_filename) + " has been tansferred through FTP");
if (directory2Upload)
{
dirPath2UploadFrom = temporary_pathDir.DirectoryName + @"\" + Path.GetFileNameWithoutExtension(temporary_pathDir.Name);
dirPath2Move = temporary_pathDir.DirectoryName + @"\" + r.Next(100).ToString();
dirPath2Upload2 = _remoteDirectory + @"/" + uploadfile.Directory.Name + @"/" + Path.GetFileNameWithoutExtension(temporary_pathDir.Name);
//Directory.Move(dirPath2UploadFrom, dirPath2Move);
Utils.LogFile("New Dir Name: " + dirPath2Move);
if (Directory.Exists(dirPath2UploadFrom))
{
if (replaceFileOnTransfer)
{
transferResult = _session.PutFiles(dirPath2UploadFrom, dirPath2Upload2, true, SetTOptions());
Utils.LogFile("[Info] [DeleteFile]: Deleted Directory: " + dirPath2Upload2 + " as replaceFileOnTransfer is True");
transferResult.Check();
}
else
{
if (_session.ListDirectory(dirPath2UploadFrom) != null)
{
Utils.LogFile("[Info] [UploadFile]: Movie Name " + Path.GetFileName(_filename) + " already exists on the remote location and has not been transferred");
return true;
}
else
{
transferResult = _session.PutFiles(dirPath2UploadFrom, dirPath2Upload2, false, SetTOptions());
transferResult.Check();
}
}
}
// Directory.Move(dirPath2Move, dirPath2UploadFrom);
Utils.LogFile("[Info] [UploadFile]: Movie Name " + Path.GetFileName(_filename) + " fragments been tansferred through FTP");
}
File.Delete(temporary_path);
Utils.LogFile("Deleted new temporary file");
return true;
}
catch (Exception ex)
{
Utils.LogFile("Exception on File Upload: " + ex.Message);
throw ex;
}
}
private bool DeleteFile(string f)
{
File.Delete(f);
return true;
}
/// <summary>
///
/// </summary>
public void KillSession()
{
Process[] _process = Process.GetProcessesByName("WinSCP");
try
{
_process.All(Kill);
Utils.LogFile("[Info] [KillWINSCP] WINSCP EXE process closed and Killed");
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
///
/// </summary>
/// <param name="_p"></param>
private bool Kill(Process _p)
{
_p.Kill();
_p = null;
if (_p == null)
return true;
else
return false;
}
}