Vous êtes sur la page 1sur 3

using System;

using System.Net;
using System.IO;

namespace OPI.OnBaseToiVaultExportService.BLL
{
class opisftp
{
public opisftp()
{
psftpCommandTimeOut = 5000 * 60;
psftpRootPath = ""; // "/users/ftpsap"
}
public string sftpDNS { get; set; }
public string sftpFolderPath { get; set; }
public string sftpUsername { get; set; }
public string sftpPassword { get; set; }
public string errorMessage { get; set; }
public string sftpStdOutputMessage { get; set; }
public string sftpStdErrorMessage { get; set; }
public string psftpFilePath { get; set; }
public string psftpCommandFilePath { get; set; }
public Int32 psftpCommandTimeOut { get; set; }
public string psftpRootPath { get; set; }

public bool Upload(string filename)


{
FileInfo fileInf = new FileInfo(filename);
if (SFTPFiles(filename))
{
try
{
if (Directory.Exists(fileInf.DirectoryName + "\\Archive"))
fileInf.MoveTo(fileInf.DirectoryName + "\\Archive\\" +
fileInf.Name);
else
{
Directory.CreateDirectory(fileInf.DirectoryName +
"\\Archive");
if (File.Exists(fileInf.DirectoryName + "\\Archive\\" +
fileInf.Name))
File.Delete(fileInf.DirectoryName + "\\Archive\\" +
fileInf.Name);
fileInf.MoveTo(fileInf.DirectoryName + "\\Archive\\" +
fileInf.Name);
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
return true;
}
else
{
return false;
}
}
public bool SFTPFiles(string filename)
{
try
{
//----------------------------------------------------------------
-------\\
if (!(psftpCommandFilePath.Contains("SFTPCommand.txt")))
psftpCommandFilePath = psftpCommandFilePath +
"\\SFTPCommand.txt";
TextWriter ObjTextWriter = new StreamWriter(psftpCommandFilePath,
false);
ObjTextWriter.WriteLine("cd " + sftpFolderPath);
ObjTextWriter.WriteLine("put \"" + filename + "\"");
ObjTextWriter.WriteLine("bye");
ObjTextWriter.Close();
//----------------------------------------------------------------
-------\\
System.Diagnostics.ProcessStartInfo ObjInfo = new
System.Diagnostics.ProcessStartInfo();
ObjInfo.FileName = psftpFilePath;
ObjInfo.Arguments = "\"" + sftpDNS + "\"" + " -l " + "\"" +
sftpUsername + "\"" + " -pw " + " " + sftpPassword + " " + " -batch" + " " + " -b
" + "\"" + psftpCommandFilePath + "\"";
ObjInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
ObjInfo.UseShellExecute = false;
ObjInfo.RedirectStandardOutput = true;
ObjInfo.RedirectStandardError = true;
System.Diagnostics.Process ObjProcess = new
System.Diagnostics.Process();
//----------------------------------------------------------------
-------\\
ObjProcess.StartInfo = ObjInfo;
ObjProcess.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
ObjProcess.StartInfo.CreateNoWindow = true;
ObjProcess.Start();
ObjProcess.WaitForExit(psftpCommandTimeOut);
if (!ObjProcess.HasExited)
{
ObjProcess.Kill();
}
sftpStdOutputMessage = ObjProcess.StandardOutput.ReadToEnd();
sftpStdErrorMessage = ObjProcess.StandardError.ReadToEnd();
ObjProcess.Close();
//----------------------------------------------------------------
-------\\
return ChkSftpStatus(sftpStdOutputMessage, filename,
sftpFolderPath);
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
}
public bool ChkSftpStatus(string strStdOutput, string strFile, string
strSftpFolder)
{
FileInfo fileInf = new FileInfo(strFile);
string strResponse = "";
if(strSftpFolder.Trim() != "")
strResponse = "local:@strFile => remote:" + psftpRootPath +
"/@strSftpFolder/@filename";
else
strResponse = "local:@strFile => remote:" + psftpRootPath +
"@filename";
strResponse = strResponse.Replace("@strSftpFolder", strSftpFolder);
strResponse = strResponse.Replace("@strFile", strFile);
strResponse = strResponse.Replace("@filename", fileInf.Name);
return strStdOutput.Contains(strResponse);
}
}
}

Vous aimerez peut-être aussi