有关cmd窗口命令行的执行!急

sxf832 2008-01-03 11:20:46
如何用代码实现在cmd窗口输入如下三条命令
ftp 172.16.2.15
用户名
密码
...全文
317 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
subMain 2008-05-04
  • 打赏
  • 举报
回复
请参照“http://blog.csdn.net/subMain/archive/2008/05/04/2387025.aspx”。。



//连接FTP
public int ConnectFTP(string Host, string User, string Pass)
{
try
{
//初始化Socket
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.GetHostAddresses(Host)[0], 21); //默认端口号是21

//根据IP和端口号连接FTP
ClientSocket.Connect(ep);
ReadMessage();
if (RetMsgId != 220)
{
ClientSocket = null;
return 0;
}

//登录FTP
SendCommand("USER " + User);
if (!(RetMsgId == 331 || RetMsgId == 230))
{
//FTPの接続を閉じる
//DisconnectFTP();
return 0;
}
//パスワードを送信する
SendCommand("PASS " + Pass);
if (!(RetMsgId == 230 || RetMsgId == 202))
{
//FTPの接続を閉じる
//DisconnectFTP();
return 0;
}

return 1;
}
catch (Exception ex)
{
throw ex;
}
}

AechoJohn 2008-05-04
  • 打赏
  • 举报
回复
试一下看看直接使用cmd命令能不能搞定你的问题。

set tempcmdfile=uploadcmd.txt
echo open 172.16.2.15 portnumber>%tempcmdfile%
echo 用户名>>%tempcmdfile%
echo 密码>>%tempcmdfile%

echo mput 你的文件名 >>%tempcmdfile%
echo y >>%tempcmdfile%

echo bye >>%tempcmdfile%
ftp -s:%tempcmdfile%
del %tempcmdfile%
cqunknown 2008-01-17
  • 打赏
  • 举报
回复
没做过,学习
minioreo 2008-01-05
  • 打赏
  • 举报
回复
遇到p.waitforexit()直接就退出了 顶起
changjiangzhibin 2008-01-04
  • 打赏
  • 举报
回复
vwxyzh 2008-01-04
  • 打赏
  • 举报
回复
把Console显示的东西贴出来看一下
sxf832 2008-01-04
  • 打赏
  • 举报
回复
还是不行啊,我的代码如下:
static void Main(string[] args)
{
ProcessStartInfo info = new ProcessStartInfo("telnet", "172.1.2.15");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
Process p = Process.Start(info);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);//2.0新增加的事件
p.BeginOutputReadLine();
p.StandardInput.WriteLine("docomo");
p.StandardInput.WriteLine("docomo");
p.StandardInput.WriteLine("exit");
p.WaitForExit();
Console.WriteLine("Process exited.");
Console.ReadLine();
}

static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
sxf832 2008-01-03
  • 打赏
  • 举报
回复
看了一些例子
p.standardinput.writeline("exit");
string strrst = p.standardoutput.readtoend();
执行exit后才能得到返回值,但这样程序就退出了。无法继续输入命令了!
-----------------------------------------------------------------------------
目的:登陆远程服务器,并运行远程服务器上的程序,根据程序执行的结果判断继续执行其他命令!
minioreo 2008-01-03
  • 打赏
  • 举报
回复
vwxyzh 2008-01-03
  • 打赏
  • 举报
回复
2.0的方法:
        static void Main(string[] args)
{
ProcessStartInfo info = new ProcessStartInfo("ftp");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
Process p = Process.Start(info);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);//2.0新增加的事件
p.BeginOutputReadLine();
p.StandardInput.WriteLine("?");
p.StandardInput.WriteLine("quit");
p.WaitForExit();
Console.WriteLine("Process exited.");
Console.ReadLine();
}

static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧