FTP到MainFrame - 需要返回代码

时间:2012-05-16 17:51:34

标签: c# asp.net windows batch-file ftp

从我的asp.net应用程序中,我正在调用批量作业,基本上将一些文件FTP到大型机。但是我无法返回FTP代码,我需要它,所以我可以知道文件是否成功发送?在StandardOutput我只得到我执行的命令,没有太多信息。请参阅下面的代码。仅供参考,我没有办法使用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

1 个答案:

答案 0 :(得分:0)

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

    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