Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2669
  • Last Modified:

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
0
mega_119
Asked:
mega_119
1 Solution
 
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
 
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now