C# FTPing到大型机-需要返回代码
从我的asp.net应用程序中,我调用了一个批处理作业,基本上是将一些文件FTPs到大型机。但是,我无法返回FTP代码,我需要它,以便我可以知道文件是否成功发送?在StandardOutput中,我只得到我执行的命令,没有多少信息。请参阅下面的代码。仅供参考,之后我无法使用GET进行验证,我想验证,但我被告知这是不可能的C# FTPing到大型机-需要返回代码,c#,asp.net,windows,batch-file,ftp,C#,Asp.net,Windows,Batch File,Ftp,从我的asp.net应用程序中,我调用了一个批处理作业,基本上是将一些文件FTPs到大型机。但是,我无法返回FTP代码,我需要它,以便我可以知道文件是否成功发送?在StandardOutput中,我只得到我执行的命令,没有多少信息。请参阅下面的代码。仅供参考,之后我无法使用GET进行验证,我想验证,但我被告知这是不可能的 ProcessStartInfo ProcessInfo; Process process; string output = string.Empty;
ProcessStartInfo ProcessInfo;
Process process;
string output = string.Empty;
string error = string.Empty;
ProcessResult item = new ProcessResult();
ProcessInfo = new ProcessStartInfo("cmd.exe", "/c" +
"ftp -n -s:myftpsettings.txt FTP.SERVER.XFHG39"
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = false;
ProcessInfo.RedirectStandardError = true;
ProcessInfo.RedirectStandardOutput = true;
process = Process.Start(ProcessInfo);
process.WaitForExit();
output = process.StandardOutput.ReadToEnd();
error = process.StandardError.ReadToEnd();
ExitCode = process.ExitCode;
process.Close();
FTP Settings
user *******
********
QUOTE SITE LRECL=80 RECFM=FB CY PRI=100 SEC=10
BIN
PUT MYFILE 'NewName'
QUIT
您是否考虑过不为此启动外部可执行文件,而是使用System.Net中的类并返回
请注意,您的ftp url类似于
ftp://ftphost.com/ftpdirectory/textfile.txt
可能尝试FTP GET,或者在PUT之后存在新文件名?它应该适用于任何标准的FTP服务器,无论它是否在大型机上。@user1106741您是否尝试过这个,如果是,它是否适用于您?如果有,请接受这个答案。
private static FtpWebRequest CreateFtpWebRequest(string ftpUrl, string userName, string password, bool useSsl, bool allowInvalidCertificate, bool useActiveFtp)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Credentials = new NetworkCredential(userName, password);
if (useSsl)
{
request.EnableSsl = true;
if (allowInvalidCertificate)
{
ServicePointManager.ServerCertificateValidationCallback = ServicePointManager_ServerCertificateValidationCallback;
}
else
{
ServicePointManager.ServerCertificateValidationCallback = null;
}
}
request.UsePassive = !useActiveFtp;
return request;
}
private static FtpStatusCode UploadFileToServer(string ftpUrl, string userName, string password, bool useSsl, bool allowInvalidCertificate, bool useActiveFtp, string filePath)
{
FtpWebRequest request = CreateFtpWebRequest(ftpUrl, userName, password, useSsl, allowInvalidCertificate, useActiveFtp);
request.Method = WebRequestMethods.Ftp.UploadFile;
long bytesReceived = 0;
long bytesSent = 0;
FtpStatusCode statusCode = FtpStatusCode.Undefined;
using (Stream requestStream = request.GetRequestStream())
using (FileStream uploadFileStream = File.OpenRead(filePath))
{
// Note that this method call requires .NET 4.0 or higher. If using an earlier version it will need to be replaced.
uploadFileStream.CopyTo(requestStream);
bytesSent = uploadFileStream.Position;
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
bytesReceived = response.ContentLength;
statusCode = response.StatusCode;
}
return statusCode;
}