尝试更多类似的东西(这来自 Windows Web 服务)。
winscp.exe 必须位于应用程序的根目录中。
编辑:请参阅 winscp.net/eng/docs/library_install “WinSCP .NET 程序集与 WinSCP winscp.exe 交互。默认情况下,它会在存储程序集的同一文件夹中查找 winscp.exe。因此,您应该将包解压到您安装/解压 WinSCP 的同一文件夹中。您还可以将所有二进制文件 winscp.exe 和 winscpnet.dll 复制到单独的文件夹中。“尝试将 .exe 放入您的应用程序文件夹中。
要将 winSCP dll 合并到您的 exe 中,请阅读Embedding DLLs in a compiled executable
using WinSCP;
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = EdiConfiguration.FtpIpAddressOrHostName,
UserName = EdiConfiguration.FtpUserName,
Password = EdiConfiguration.FtpPassword,
SshHostKeyFingerprint = EdiConfiguration.SshHostKeyFingerprint,
PortNumber = EdiConfiguration.FtpPortNumber
};
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
// Download the files in the OUT directory.
TransferOperationResult transferOperationResult = session.GetFiles(EdiConfiguration.FtpDirectory, EdiConfiguration.IncommingFilePath, false, transferOptions);
// Check and throw if there are any errors with the transfer operation.
transferOperationResult.Check();
// Remove files that have been downloaded.
foreach (TransferEventArgs transfer in transferOperationResult.Transfers)
{
RemovalOperationResult removalResult = session.RemoveFiles(session.EscapeFileMask(transfer.FileName));
if (!removalResult.IsSuccess)
{
eventLogUtility.WriteToEventLog("There was an error removing the file: " + transfer.FileName + " from " + sessionOptions.HostName + ".", EventLogEntryType.Error);
}
}
}
}
catch (SessionLocalException sle)
{
string errorDetail = "WinSCP: There was an error communicating with winscp process. winscp cannot be found or executed.";
errorDetail += Environment.NewLine + "Message:" + sle.Message;
errorDetail += Environment.NewLine + "Target Site:" + sle.TargetSite;
errorDetail += Environment.NewLine + "Inner Exception:" + sle.InnerException;
errorDetail += Environment.NewLine + "Stacktrace: " + sle.StackTrace;
eventLogUtility.WriteToEventLog(errorDetail, EventLogEntryType.Error);
}
catch (SessionRemoteException sre)
{
string errorDetail = "WinSCP: Error is reported by the remote server; Local error occurs in WinSCP console session, such as error reading local file.";
errorDetail += Environment.NewLine + "Message:" + sre.Message;
errorDetail += Environment.NewLine + "Target Site:" + sre.TargetSite;
errorDetail += Environment.NewLine + "Inner Exception:" + sre.InnerException;
errorDetail += Environment.NewLine + "Stacktrace: " + sre.StackTrace;
eventLogUtility.WriteToEventLog(errorDetail, EventLogEntryType.Error);
}
catch (Exception ex)
{
eventLogUtility.WriteToEventLog("Error in ProcessEdi() while downloading EDI files via FTP: Message:" + ex.Message + "Stacktrace: " + ex.StackTrace, EventLogEntryType.Error);
}