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

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 10.10.10.245
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
                    ftpProcess.Start()


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

Any advice
mega_119Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iboutchkineCommented:
I have recently did the same. I found out that NCFTP works much better then FTP. You can dowload NCFTP from here
http://ncftp.com

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"
        proc.Start()
    End Sub
End Class
0
mega_119Author Commented:
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
ftpProcess.Start()

Dim stWriter As StreamWriter

stWriter = ftpProcess.StandardInput
stWriter.AutoFlush = True
stWriter.WriteLine("user ftpuser ftpuser")
tWriter.WriteLine("mput " & targetDirectory& "results.rpt")
stWriter.WriteLine("y")
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.


0
CelerexCommented:
In order to wait for a process to finish doing what it's doing so that you can continue you can try the

System.Diagnostics.Process.WaitForInputIdle

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.