简体   繁体   English

从C#运行FTP命令批处理文件

[英]Run an FTP commands batch file from C#

I have written a winform app in C# which modifies FTP commands in a script file (server address etc..) and then executes a batch file that is supposed to start FTP from command prompt and download files according to script. 我已经用C#编写了一个winform应用程序,该程序可以修改脚本文件(服务器地址等)中的FTP命令,然后执行一个批处理文件,该文件应该从命令提示符处启动FTP并根据脚本下载文件。

When i run the Batch file external to app (from command prompt or double click), the files are being downloaded fine, but when executing the batch from the app nothing happens... 当我运行应用程序外部的批处理文件时(从命令提示符或双击),文件下载正常,但是从应用程序执行批处理时什么也没有发生...

I also noticed following difference: When running batch externally I see " ftp> open 10.1.1.1" in cmd window. 我还注意到以下差异:在外部运行批处理时,我在cmd窗口中看到“ ftp> open 10.1.1.1”。 When running batch from the app I see "open 10.1.1.1" in cmd window,(missing the "ftp>"). 从应用程序运行批处理时,我在cmd窗口中看到“打开10.1.1.1”(缺少“ ftp>”)。

My guess is that I'm not using System.Diagnostics.Process() correctly... 我的猜测是我没有正确使用System.Diagnostics.Process()...

C# relevant part: C#相关部分:

    private void button1_Click(object sender, EventArgs e)
    {
            var cmd = new System.Diagnostics.Process();
            cmd.StartInfo.FileName = "runFTP.bat";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.WorkingDirectory = path;
            cmd.Start();
            cmd.WaitForExit();
            cmd.Close();
        }

    }

Batch file (runFTP.bat): 批处理文件(runFTP.bat):

echo off

@echo Downloading files...
REM ==Start FTP with script==

ftp -i -s:ftpCmd.txt

del ftpCmd.txt

@echo Done!
@echo Exiting...

FTP script file (ftpCmd.txt): FTP脚本文件(ftpCmd.txt):

open 10.1.1.1
user
password
bin
cd /rootFolder/new
lcd C:\Downloads
mget *.*
bye

I found a solution to my problem.. works fine now :) 我找到了解决我的问题的方法。

By adding and changing in C# the following System.Diagnostics.Process() StartInfo parameters to false, I was able to download the files and get the missing "ftp>" on command prompt. 通过在C#中添加以下System.Diagnostics.Process()StartInfo参数并将其更改为false,我可以下载文件并在命令提示符下获取缺少的“ ftp>”。

Modified C# code looks like this 修改后的C#代码如下所示

            var cmd = new System.Diagnostics.Process();
            cmd.StartInfo.FileName = "runFTP.bat";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.RedirectStandardInput = false;
            cmd.StartInfo.RedirectStandardOutput = false;
            cmd.StartInfo.WorkingDirectory = path;
            cmd.Start();
            cmd.WaitForExit();
            cmd.Close();

Thanks to anyone spent time on this.. hope this will contribute to someone else. 多亏有人在此上花费了时间。.希望这会有助于其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM