FTP Upload Question (using wininet.dll)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I am programming using VB.NET 2003 and
cannot use other versions such as VB 2005.


Using 'wininet.dll', I have made a FTP upload() fuction which
performs
two actions: 1)creates a new directory on FTP server and then 2)
transferes a file into the directory. If the program calls the
function once, it works fine. But if the function is called twice or
more in a program running, it fails to transfer the file to the FTP
server.


When it was called for the first time, it creates a directory, called
'NEW', under FTP server's root working directory and transfers a
file,
'1st.txt', into the cerated directory.


On the second time when it's called, it seems that it tries to create
the directory, 'NEW', again, but the directory already exists.
Therefore, the attempt to create a new directory fails(ie.,
'FtpCreateDirectory' returns 'False').


After the attempt to create 'NEW' directory fails, it tries to
transfer a file, 2nd.txt', into 'NEW' directory. But this attempt
also
fails (ie., 'FtpPutFile' returns 'False').


I have two questions about this behavior:
1) Before trying to create a directory on FTP server, is it possible
to check if the directory already exists and, if it does, do nothing?
I mean, can I make the program such that it just skips the attempt to
create the directory if the directory it's trying to create already
exists?


2) I think, when it's called for the second time, even if the attempt
to create the folder failed, the attempt to transfer the file should
succeed becuase the folder already exists and there should be nothing
wrong with the FtpPutFile method. But this attempt also fails (ie.,
'FtpPutFile' returns 'False') Why?


Below is the Upload() function I have. I have checked the variables
used in the function contain correct values in runtime.


Please have a look and let me know what is wrong.
I am using VB.NET 2003 and cannot use other versions such as VB 2005.
Thank you very much in advance for your help.


Sub Upload(ByVal NewFileName As String, ByVal srcFilePath As
String, _
ByVal destFTPAddr As String, ByVal ID As String, ByVal
PASS As String)
Dim INet, INetConn As Integer
Dim RC, CD As Boolean
Dim RemoteNewdirectory As String


RemoteNewdirectory = "./NEW" + "/"
destPath = RemoteNewdirectory + NewFileName


INet = InternetOpen("FTP Connection",
INTERNET_OPEN_TYPE_PRECONFIG, vbNullString,_
vbNullString, 0)
INetConn = InternetConnect(INet, destFTPAddr, 21, ID, PASS,
INTERNET_SERVICE_FTP,_
INTERNET_FLAG_PASSIVE, 0)
CD = FtpCreateDirectory(INetConn, RemoteNewdirectory)
RC = FtpPutFile(INetConn, srcFilePath, destPath,
FTP_TRANSFER_TYPE_BINARY, 0)


'If RC Then MsgBox("File uploaded!")
If Not RC Then
err =
System.Runtime.InteropServices.Marshal.GetLastWin32Error()
MsgBox("Error occured")
InternetCloseHandle(INetConn)
End If
InternetCloseHandle(INet)
End Sub
.



Relevant Pages

  • RE: Log-In Credentials in a Macro
    ... I would try FTP instead. ... cFileName As String * MAX_PATH ... Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias ... Public Declare Function InternetWriteFile Lib "wininet.dll" _ ...
    (microsoft.public.excel.programming)
  • Re: File.Create - ArgumentException was unhandled - illegal characters in path
    ... No probs - It relates to an FTP class - it ... Sub FTP_DownloadFolderContents(ByVal ftpFolder As String, ... Dim sSourceFiles As String ...
    (microsoft.public.dotnet.languages.vb)
  • FTP Upload error (WININET.DLL)
    ... I have made a FTP upload() fuction which performs ... it fails to transfer the file to the FTP ... Before trying to create a directory on FTP server, ... Sub Upload(ByVal NewFileName As String, ...
    (microsoft.public.dotnet.languages.vb)
  • [VB.NET 2003] FTP upload suceeded. but no file transferred?
    ... I wrote a simple FTP code which monitors a user-specified folder and, ... the FTP server program says it has sucessfully received the file. ... "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FTP CD command
    ... FTP client once in the time that the framework did not provide anny FTP ... Private _Uri As String ... Public Sub New(ByVal Uri As String, ... Dim listRequest As FtpWebRequest = CType, ...
    (microsoft.public.dotnet.languages.vb)