如果这是一个愚蠢的错误,请原谅我。我几乎没有使用CMD的经验,也没有从C#程序中执行cmd的经验。我想用我的C#代码以编程方式执行FTP命令,将文件打印到PDF。我遇到的问题是,当被要求登录AS400时,我认为我的程序在发送下一行(即USERNAME)之前没有等待足够长的时间来建立连接。所以下一行无法登录。
如果我在cmd中逐行执行此操作,它就可以正常工作。
以下是我所拥有的:
这是我到目的地文件夹的目录:
string StatementString = "\\Varying Directories To User Folder\\Directory with spaces\\Destination";
string ToUserDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string StatementPath = ToUserDirectory + StatementString;
StringBuilder shortPath = new StringBuilder(255);
GetShortPathName(StatementPath, shortPath, shortPath.Capacity);
Process CMDScript = new Process();
var processInfo = new System.Diagnostics.ProcessStartInfo();
processInfo.FileName = "cmd.exe";
processInfo.RedirectStandardInput = true;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
CMDScript.StartInfo = processInfo;
CMDScript.Start();
using (StreamWriter sw = CMDScript.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
//"cd \PathDirectory\ [- C:]
sw.WriteLine("cd" + shortPath.ToString().Substring(2));
//Delete Old Files
sw.WriteLine("del /Q *");
//Connects to my AS400 using its IP Address (Using Zeroes for this question)
sw.WriteLine("ftp 000.000.00.00");
sw.WriteLine("USERNAME");
sw.WriteLine("PASSWORD");
//Directory of stuff I want from AS400
sw.WriteLine("cd /home/reallycoolplacewithstuffIneed");
//Back to my Directory on my PC
sw.WriteLine("lcd" + shortPath.ToString().Substring(2));
//Get the stuff I need
sw.WriteLine("get statements.csv");
//Exit the command prompt
sw.WriteLine("bye");
}
}
CMDScript.OutputDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) =>
Console.WriteLine("output>>" + e.Data);
CMDScript.BeginOutputReadLine();
CMDScript.ErrorDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) =>
Console.WriteLine("error>>" + e.Data);
CMDScript.BeginErrorReadLine();
CMDScript.WaitForExit();
Console.WriteLine("ExitCode: {0}", CMDScript.ExitCode);
CMDScript.Close();
我希望这个问题足够明确。我只需要一种安全的登录AS400的方法,这样我就可以使用FTP了。我不确定在建立连接时是否需要一些Wait(1)方法来暂停我的程序,或者用户和密码的处理方式是否不同。到目前为止,其他一切似乎都运转得很好,而不是连接。
答案 0 :(得分:1)
在https://technet.microsoft.com/en-us/sysinternals/ms229711(v=vs.90)查看Technet文章“如何:使用FTP下载文件”,它包含一个C#代码段。
我尝试将其编辑为您在示例中提供的信息。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://000.000.000.000/home/reallycoolplacewithstuffIneed");
request.Method = WebRequestMethods.Ftp.DownloadFile;
// Trying to use your credentials
request.Credentials = new NetworkCredential("USERNAME","PASSWORD");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
}
}
希望有所帮助。
小心
安德烈亚斯