我使用命令行(視窗)將文件發送到IBM主機和工作正常,它是這樣的:使用C#FTP文件到大型機,包括數據集 - 翻譯FTP腳本的FtpWebRequest代碼
Open abc.wyx.state.aa.bb
User
Pass
lcd c:\Transfer>
Put examplefile 'ABCD.AA.C58FC.ABC1FD.ZP3ABC'
close
bye
我需要將其轉換爲C#。我一直在嘗試使用FtpWebRequest
,但沒有運氣。我無法弄清楚如何包含我猜測的數據集。當我運行應用程序,我得到了以下錯誤:
((System.Exception)(ex)).Message "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." 550 Unable to store The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
((FtpWebResponse)ex.Response).StatusDescription "550 Unable to store /'ABCD.AA.C58FC.ABC1FD.ZP3ABC/examplefile'\r\n"
以下是我在C#
string user = "user";
string pwd = "password";
string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C58FC.ABC1FD.ZP3ABC'/examplefile'";
try
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(user, pwd);
ftp.KeepAlive = true;
ftp.UseBinary = false; //Use ascii.
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (WebException ex)
{
String status = ((FtpWebResponse)ex.Response).StatusDescription;
throw new Exception(status);
}
您是否考慮過查看'Process.Start'來運行批處理文件,或者您想使用嚴格的C#代碼發送它?你有沒有使用Google搜索如何通過FTP連接和發送文件..有大量的在線例子 – MethodMan
是的,它必須在C#上,是的,我看的鏈接,但我找不到它對我有幫助。我正在使用與我的批處理文件相同的憑據,它們在上傳文件時沒有問題。 –
我只是更新錯誤消息並用虛擬名稱替換x。 examplefiles是我在cmd腳本上使用的真實名稱並且可以工作。 –