Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP in VB in VB 2005 Express 1

Status
Not open for further replies.

mikej336

MIS
Feb 10, 2005
164
US
Hey, Newbie question.

I need some very basic ftp funcionality. I need to ftp a file to the server wait a few seconds and ftp back anouther file.

I have looked a CFtpClient as an option and edtFTPnet. I found both to be confusing.

Anybody have any suggestions or an example project.

Any thought appreciated.

Uncle Mike
 
The idea:

1. Create a batch file.
2. Run ftp based on the infos in the batch file.

Code:
Dim File2Upload As String = "C:\MyTest.DAT"
Dim fname As String = "MyBatch.tmp"
Dim hWriter As IO.StreamWriter
hWriter = New IO.StreamWriter(fname, True)
'turn interactive mode OFF
hWriter.WriteLine("prompt")
hWriter.WriteLine("open MyFTPServer")
hWriter.WriteLine("MyFTPServerUsername")
hWriter.WriteLine("MyFTPServerPassword")
'change directory as needed
hWriter.WriteLine("cd MyDirectory")
'even create a directory
hWriter.WriteLine("mkdir MySubDirectory")
'get into that sub dir
hWriter.WriteLine("cd MySubDirectory")
'either use put or mput here
hWriter.WriteLine("put " & File2Upload)
'close connection
hWriter.WriteLine("bye")
'close ftp command
hWriter.Close()
Dim X As Double
X = Shell("ftp -s:" & fname, AppWinStyle.Hide, True)
Try
   'for security reason, delete the batch file
   IO.File.Delete(fname)
Catch
   'ignore any errors?
   Exit Try
End Try

Hope this helps.

mansii
 
mansii u da man...

Easy, code that actually works.

Thanks again

 
Rick,

I looked at that but I could not get the examples to work.

Do you have any?

Uncle Mike
 
I'm stuck in VS 2k2 (framework 1.0) so I had to massage it a little to work for our set up. If you are using VS 2k5 (framework 2.0) you may have to massage it some to get it to compile in 2.0. Check the forums on their site, I wouldn't be suprised if someone else already did so and posted the code changes.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I am just getting into vb.net, but can you no longer access the wininet.dll?

I used to use a similar method to mansii's in VB6, but I have found using functions from this dll much more reliable. I hope that I can still use it in VB.net???

If this is possible, I may be able to share some vb6 code that you could hack into a workable .net solution.

If it is not possible, I had better get up on the solution proposed by ThatRickGuy!

Any feedback appreciated on this one.

Thanks,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Alex

I finally figured out the page I referenced above....

************
Put this in the declaration section of a module.
************
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer
Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean

**********************
Put this in a button
**********************
Dim INet, INetConn As integer
Dim RC As Boolean
iNet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)
INetConn = InternetConnect(INet, " 0, "yourlogin", "yourpassword", 1, 0, 0)
RC = FtpGetFile(INetConn, "/folder/subfolder/name.ext", "c:\tmp\downloaded.ext", true, 1, 0, 0)
If RC Then MessageBox.show( "Transfer succesfull!")
InternetCloseHandle INetConn
InternetCloseHandle INet
*****************


And it worked for me.

Uncle Mike
 
OK... It works for windows.... Does not appear to work with an AS400. Unless anyone know how to tell it to change directories.

FtpSetCurrentDirectory(INetConn, "QOpenSys")

does not appear to work.

Uncle Mike
 
I still think you'll be better off just moving to a managed code solution. If DT Enterprise doesn't have a 2.0 library, try this one: If not that, then dig around on the web a bit, there are a number of options out there.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Alex.. I figured out that it does work with the AS400 you just have to use the relative path and not try to change directories like a normal ftp session, like....

/QDLS/MikeTest/test.jpg

But am confident that it will not allow you to pass FTP commands to an AS400.

But it appears to handle my project.

Uncle Mike
 
Thanks Rick, that should come in handy when/if I start using VB.net as more than a 'hobby' (knowing my company, by 2009 this should happen ;-) ). It appears less convoluted than jumping through all the hoops to use the DLL, and hopefully it is more robust as well.

Mike - What do you mean by change directories? I think what you are trying to do may be possible, but only if I am right about what it in fact is...

That being said, I think that Rick's last suggestion is the way to go, because it takes advantage of the System.net classes that appear to offer much more.

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top