Pass input parameter to a process using Process.StandardInput.WriteLn

Posted on 2003-03-29
Medium Priority
Last Modified: 2010-07-27
Hi Everyone..

I have struggled for days with this one.

I have written a vb code to run a bat file to start an ftp process, the problem is that I need to input the user name, and password and then after that I need to transfer a file (using put command).

When I execute the ftp command as the following I have no problem
ftp -s:Filename.txt
where FileName.txt contains the command I wish to execute right after FTP starts ( including the authentication).

This solution will not work for me since the file name that I am transferring varies from within the program and I cannot hardcode it into the text file.

The System.Diagnostics.process and System.Diagnostics.ProcessStartInfo allow to write input to the process , and this is what I tried in the following code but it was unsucessfull.

                    Dim pStart As New System.Diagnostics.ProcessStartInfo()
                    pStart.FileName = Directory.GetCurrentDirectory & "\ftpLog.bat"

                    pStart.RedirectStandardInput = True
                    pStart.UseShellExecute = False

                    Dim ftpProcess As New Process()
                    ftpProcess.StartInfo = pStart

                    Dim stWriter As StreamWriter()
                    ftpProcess.StandardInput.AutoFlush = True
                    ftpProcess.StandardInput.WriteLine("mput c:\tempdir\" + strFileName)

Any advice
Question by:mega_119
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Expert Comment

ID: 8233800
I have recently did the same. I found out that NCFTP works much better then FTP. You can dowload NCFTP from here

here you can find all the options for upload/download

Uou will find ther the syntax for upload/download. For example the upload ( batch file)
ncftpput [options] remote-host "remote-directory" "local-files..."

ncftpput -Z -u username -p password IPAddress "PathOnServer" "PathOnClient"
 and the call the batch file like that

Imports System.Diagnostics.Process
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proc As New Process()

        proc.StartInfo.FileName = "c:\temp\YourBaychFile.bat"
    End Sub
End Class

Author Comment

ID: 8236521
I did manage to get ftp working. The problem was typing in the password, I could not redirect the input when it was the password, I had no problem with the user name. The way I solved by using -n argument in ftp and then using "user <id> <password>" in the ftp session to authenticate

ftp -n <ip> (suppress auto-login upon initial connection)

I found out that when I redirect the output as well as the input I do not get anything written to the window although the FTP succeed. This prevented me from reading the output.

The code would look something like that

pStart.FileName = Directory.GetCurrentDirectory & "\ftpLog.bat"
pStart.RedirectStandardInput = True
pStart.UseShellExecute = False
                                       ftpProcess.StartInfo = pStart

Dim stWriter As StreamWriter

stWriter = ftpProcess.StandardInput
stWriter.AutoFlush = True
stWriter.WriteLine("user ftpuser ftpuser")
tWriter.WriteLine("mput " & targetDirectory& "results.rpt")
stWriter.WriteLine("bye") ''' **** I cannot close the session unless I know that the ftp complete

The problem with this code is:
I have to wait for the ftp to finish (the file is big around 100 MB and will take a while) before I can close the ftp session and then the process.

I need to check the ftp session has completed and then after that stop the FTP Publishing Service, or at least close the process.


Accepted Solution

Celerex earned 100 total points
ID: 8741982
In order to wait for a process to finish doing what it's doing so that you can continue you can try the


I've tried it, not in VB but in C# and it should bei nVB as well. Best of luck.
LVL 96

Expert Comment

by:Bob Learned
ID: 9725674
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by Celerex

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question