So I have this app that I am converting to a windows service so it can run unattended. While I was building this as an app with a GUI this portion of the code works just fine. When I migrated the code over to a windows service, it looks as if it works, however the file does not get uploaded.
original code from here
FTP from inside VB.NET Program
Code:
Private 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
Private 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
Private 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
Private 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
Public Sub test(ByVal sFileName As String, ByVal fiFullName As String, ByVal sOrgPath As String, ByVal sNewPath As String)
Dim ServerName As String
Dim sPort As Integer
Dim FTPLogin As String
Dim FTPPassword As String
' --------------------
' Transfer via standard FTP
ServerName = ""
sPort =
FTPLogin = ""
FTPPassword = ""
Try
' Instantiate a new FTP object
Dim INet, INetConn As Integer
Dim RC As Boolean
' FTP_TRANSFER_TYPE_ASCII
' FTP_TRANSFER_TYPE_BINARY
INet = InternetOpen("DMGImport_FTP", 1, vbNullString, vbNullString, 0)
INetConn = InternetConnect(INet, ServerName, sPort, FTPLogin, FTPPassword, 1, 0, 0)
RC = FtpPutFile(INetConn, fiFullName, sNewPath & sFileName, 0, 0)
If RC Then
'MsgBox("Transfer succesfull!")
End If
InternetCloseHandle(INetConn)
InternetCloseHandle(INet)
Catch ex As Exception
End Try
End Sub
so to recap,
When used as an application that I have to interact with the file is uploaded
When I change this to a service, and set the service to run with my credentials, the file is not uploaded nor is there an error.
What could cause this? When I run the code in debug, everything looks good. The server I am uploading to is a Linux box.
Thanks