FTP 到大型机 - 需要返回代码

本文关键字:返回 代码 大型机 FTP | 更新日期: 2023-09-27 18:34:27

从我的 asp.net 应用程序中,我正在调用一个批处理作业,该作业基本上是将几个文件FTP到大型机。但是我无法返回FTP代码,我需要它,以便我知道文件是否成功发送?在标准输出中,我只得到我执行的命令,没有太多信息。有关代码,请参见下文。仅供参考,我无法在之后使用 GET 进行验证,我想这样做,但我被告知这是不可能的!

    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

FTP 到大型机 - 需要返回代码

您是否考虑过不为此使用外部可执行文件,而是使用 System.Net 中的类并返回 FtpStatusCode 枚举?

    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;
    }

请注意,您的 ftp 网址类似于 ftp://ftphost.com/ftpdirectory/textfile.txt .