0

从我的 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
4

1 回答 1

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.

于 2012-05-16T20:46:33.703 回答