ftp
open 10.89.53.231 22
bqc\robbie.zhu
!xxxx
cd /FtpTest/T1
put D:\Project\MsProject\欧尚V3.6\实现3.6P1版本\PresentationLayer\ePayroll\SOBFiles\IEF001EHR001361.dat
quit
exit
public bool UploadFileToFTPByCmd(string fileFullName)
{
string _strFtpIP = ConfigurationSettings.AppSettings["FTPSettingIP"].ToString().Trim();
string _strFtpPort = ConfigurationSettings.AppSettings["FTPSettingPort"].ToString().Trim();
string _strFtpUserName = ConfigurationSettings.AppSettings["FTPSettingUserName"].ToString().Trim();
string _strFtpPassword = ConfigurationSettings.AppSettings["FTPSettingPassrword"].ToString().Trim();
string _strFtpFoloderPath = ConfigurationSettings.AppSettings["FTPSettingFolderPath"].ToString().Trim();
if(!File.Exists(fileFullName))
{
base.ShowAlert("文件未生成成功,请检查!");
return false;
}
else
{
FileStream fs = null;
string _strScriptFolderPath = Path.GetDirectoryName(fileFullName) + "\\FtpScript";
if(!Directory.Exists(_strScriptFolderPath))
{
Directory.CreateDirectory(_strScriptFolderPath);
}
string _strScriptFilePath = _strScriptFolderPath + @"\FtpScript" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
if(File.Exists(_strScriptFilePath))
{
fs = new FileStream(_strScriptFilePath,FileMode.Append);
}
else
{
fs = new FileStream(_strScriptFilePath,FileMode.Create);
}
//ftp示例文件内容
//open 10.89.53.231 22
//bqc\robbie.zhu
//!free12345
//cd FtpTest
//put d:\06.txt
//quit
//exit
string _strOpenFtpUrl = "open " + _strFtpIP + " " + _strFtpPort;
string _strCdFtpFolderPath = "cd " + _strFtpFoloderPath;
string _strPutFile = "put " + fileFullName;
StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding("GB2312"));
sw.WriteLine("ftp");
sw.WriteLine(_strOpenFtpUrl);
sw.WriteLine(_strFtpUserName);
sw.WriteLine(_strFtpPassword);
sw.WriteLine(_strCdFtpFolderPath);
sw.WriteLine(_strPutFile);
sw.WriteLine("quit");
sw.WriteLine("exit");
sw.Close();
string _strCommand = @"FTP -v -i -s:" + _strScriptFilePath;
string _strReturnValue = DosOperation.RunCmd(_strCommand);
if(_strReturnValue.ToUpper().IndexOf("TRANSFER COMPLETE")<0)
{
base.ShowAlert("Ftp抛转失败,请检查!");
return false;
}
}
return true;
}
public class DosOperation
{
public DosOperation()
{
//
// TODO: Add constructor logic here
//
}
public static string RunCmd(string command)
{
Process p = null;
try
{
//实例一个Process类,启动一个独立进程
p = new Process();
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性:
p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.Arguments = "/c " + command; //设定程式执行参数
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口
p.Start(); //启动
p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程式执行的时候会当机
return p.StandardOutput.ReadToEnd(); //从输出流取得命令执行结果
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (p != null)
p.Close();
}
}
}