ASP.NET中调用cmd命令传输文件到FTP

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();
			}
		}   
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值