FTP

J

Jim Edgar

Is is possible to use WinInet in vb.net 2003 to download files from an FTP server? I've
been asked to rewrite some VB6 code as a service that downloads files then parses and
reformats them. The parsing and formatting was easy but the FTP stuff has me stuck.
FtpFindFirstFile() doesn't return the name of the file in the WIN32_FIND_DATA structure so
I can't get a list of the files on the server. Has anyone used WinInet before for FTP?
Is this doable in .net or do I need to use System.Net and System.Net.Sockets?

Thanks,

Jim Edgar
 
J

Jim Edgar

Thanks Altman. I had found that link on google and I'm probably going to tackle it if
WinInet isn't a choice.

Jim Edgar
 
T

Tom Shelton

Is is possible to use WinInet in vb.net 2003 to download files from an FTP server? I've
been asked to rewrite some VB6 code as a service that downloads files then parses and
reformats them. The parsing and formatting was easy but the FTP stuff has me stuck.
FtpFindFirstFile() doesn't return the name of the file in the WIN32_FIND_DATA structure so
I can't get a list of the files on the server. Has anyone used WinInet before for FTP?
Is this doable in .net or do I need to use System.Net and System.Net.Sockets?

Thanks,

Jim Edgar

Yes it's possible... I haven't really done it from VB.NET - but I have
a whole class written in C# that does it. The code should convert to
VB.NET fairly easily, and can be used from a separate library as is...

Anyway, here is my declarations from that libary, with a little main
function that does what you're asking - it seems to work here any way (I
tested this against an SCO Unix server and a FreeBSD server)...

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Testing
{
/// <summary>
/// Summary description for WinInet.
/// </summary>
public sealed class WinInet
{
public const int INTERNET_OPEN_TYPE_PRECONFIG = 0;
public const int INTERNET_OPEN_TYPE_DIRECT = 1;
public const int INTERNET_OPEN_TYPE_PROXY = 3;
public const short INTERNET_DEFAULT_FTP_PORT = 21;
public const int INTERNET_SERVICE_FTP = 1;
public const int FTP_TRANSFER_TYPE_ASCII = 0x01;
public const int FTP_TRANSFER_TYPE_BINARY = 0x02;
public const int GENERIC_WRITE = 0x40000000;
public const int GENERIC_READ = unchecked((int)0x80000000);
public const int MAX_PATH = 260;
public const int INTERNET_FLAG_NEED_FILE = 0x00000010;

[StructLayout (LayoutKind.Sequential)]
public struct FILETIME
{
public int dwLowDateTime;
public int dwHighDateTime;
}

[StructLayout (LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct WIN32_FIND_DATA
{
public int dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;

[MarshalAs (UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public string cFileName;

[MarshalAs (UnmanagedType.ByValTStr, SizeConst=14)]
public string cAlternateFileName;
}

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr InternetOpen(
string lpszAgent,
int dwAcessType,
string lpszProxyName,
string lpszProxyBypass,
int dwFlags);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr InternetConnect(
IntPtr hInternet,
string lpszServerName,
short nServerPort,
string lpszUserName,
string lpszPassword,
int dwService,
int dwFlags,
ref int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpGetCurrentDirectory(
IntPtr hConnect,
StringBuilder lpszCurrentDirectory,
ref int lpdwCurrentDirectory);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpSetCurrentDirectory(
IntPtr hConnect,
string lpszCurrentDirectory);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr FtpOpenFile(
IntPtr hConnect,
string lpszFileName,
int dwAccess,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetWriteFile(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int dwNumberOfBytesToWrite,
out int lpdwNumberOfBytesWritten);

[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetReadFile(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int dwNumberOfBytesToRead,
out int lpdwNumberOfBytesRead
);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool InternetCloseHandle(IntPtr hInternet);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpPutFile (
IntPtr hConnect,
string lpszLocalFile,
string lpszNewRemoteFile,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpGetFile (
IntPtr hConnect,
string lpszRemoteFile,
string lpszLocalFile,
bool failIfExists,
int dwFlagsAttributes,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpCreateDirectory(
IntPtr hConnect,
string lpszDirectory);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr FtpFindFirstFile(
IntPtr hConnect,
string lpszSearchFile,
out WIN32_FIND_DATA lpFindFileData,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool InternetFindNextFile(
IntPtr hFind,
out WIN32_FIND_DATA lpvFindData
);

[STAThread ()]
public static void Main ()
{
int context = 0;
IntPtr hInternet = InternetOpen (null,
INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
IntPtr hConnection = InternetConnect (
hInternet,
"zorkmid.dakcs.com",
INTERNET_DEFAULT_FTP_PORT,
"tom",
"vws4ever",
INTERNET_SERVICE_FTP,
0,
ref context);

WIN32_FIND_DATA findData;
IntPtr hFind = FtpFindFirstFile (
hConnection,
"*",
out findData,
INTERNET_FLAG_NEED_FILE,
out context
);
Console.WriteLine (findData.cFileName);
while (InternetFindNextFile (hFind, out findData))
{
Console.WriteLine (findData.cFileName);
}
InternetCloseHandle (hFind);
InternetCloseHandle (hConnection);
InternetCloseHandle (hInternet);


}
}
}

HTH
 
J

Jim Edgar

Thanks Tom --

I've never used .net before so translating some of the code is hit and miss. Here's
my current configuration of WIN32_FIND_DATA

Public Structure WIN32_FIND_DATA
Dim dwFileAttributes As Integer
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Integer
Dim nFileSizeLow As Integer
Dim dwReserved0 As Integer
Dim dwReserved1 As Integer
<VBFixedString(260)> Public cFileName As String
<VBFixedString(14)> Public cAlternate As String
End Structure

I've also tried

Public Structure WIN32_FIND_DATA
Dim dwFileAttributes As Integer
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Integer
Dim nFileSizeLow As Integer
Dim dwReserved0 As Integer
Dim dwReserved1 As Integer
<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByVa
lTStr, SizeConst:=260)> Public cFileName As String
<VBFixedString(14),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByVa
lTStr, SizeConst:=14)> Public cAlternate As String
End Structure

Which one should I use with WinInet or is there a better way
to declare fixed length strings in structures?

Thanks,
Jim Edgar

Tom Shelton said:
Is is possible to use WinInet in vb.net 2003 to download files from an FTP server? I've
been asked to rewrite some VB6 code as a service that downloads files then parses and
reformats them. The parsing and formatting was easy but the FTP stuff has me stuck.
FtpFindFirstFile() doesn't return the name of the file in the WIN32_FIND_DATA structure so
I can't get a list of the files on the server. Has anyone used WinInet before for FTP?
Is this doable in .net or do I need to use System.Net and System.Net.Sockets?

Thanks,

Jim Edgar

Yes it's possible... I haven't really done it from VB.NET - but I have
a whole class written in C# that does it. The code should convert to
VB.NET fairly easily, and can be used from a separate library as is...

Anyway, here is my declarations from that libary, with a little main
function that does what you're asking - it seems to work here any way (I
tested this against an SCO Unix server and a FreeBSD server)...

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Testing
{
/// <summary>
/// Summary description for WinInet.
/// </summary>
public sealed class WinInet
{
public const int INTERNET_OPEN_TYPE_PRECONFIG = 0;
public const int INTERNET_OPEN_TYPE_DIRECT = 1;
public const int INTERNET_OPEN_TYPE_PROXY = 3;
public const short INTERNET_DEFAULT_FTP_PORT = 21;
public const int INTERNET_SERVICE_FTP = 1;
public const int FTP_TRANSFER_TYPE_ASCII = 0x01;
public const int FTP_TRANSFER_TYPE_BINARY = 0x02;
public const int GENERIC_WRITE = 0x40000000;
public const int GENERIC_READ = unchecked((int)0x80000000);
public const int MAX_PATH = 260;
public const int INTERNET_FLAG_NEED_FILE = 0x00000010;

[StructLayout (LayoutKind.Sequential)]
public struct FILETIME
{
public int dwLowDateTime;
public int dwHighDateTime;
}

[StructLayout (LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct WIN32_FIND_DATA
{
public int dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;

[MarshalAs (UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public string cFileName;

[MarshalAs (UnmanagedType.ByValTStr, SizeConst=14)]
public string cAlternateFileName;
}

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr InternetOpen(
string lpszAgent,
int dwAcessType,
string lpszProxyName,
string lpszProxyBypass,
int dwFlags);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr InternetConnect(
IntPtr hInternet,
string lpszServerName,
short nServerPort,
string lpszUserName,
string lpszPassword,
int dwService,
int dwFlags,
ref int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpGetCurrentDirectory(
IntPtr hConnect,
StringBuilder lpszCurrentDirectory,
ref int lpdwCurrentDirectory);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpSetCurrentDirectory(
IntPtr hConnect,
string lpszCurrentDirectory);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr FtpOpenFile(
IntPtr hConnect,
string lpszFileName,
int dwAccess,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetWriteFile(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int dwNumberOfBytesToWrite,
out int lpdwNumberOfBytesWritten);

[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetReadFile(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int dwNumberOfBytesToRead,
out int lpdwNumberOfBytesRead
);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool InternetCloseHandle(IntPtr hInternet);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpPutFile (
IntPtr hConnect,
string lpszLocalFile,
string lpszNewRemoteFile,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpGetFile (
IntPtr hConnect,
string lpszRemoteFile,
string lpszLocalFile,
bool failIfExists,
int dwFlagsAttributes,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool FtpCreateDirectory(
IntPtr hConnect,
string lpszDirectory);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern IntPtr FtpFindFirstFile(
IntPtr hConnect,
string lpszSearchFile,
out WIN32_FIND_DATA lpFindFileData,
int dwFlags,
out int dwContext);

[DllImport("wininet.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool InternetFindNextFile(
IntPtr hFind,
out WIN32_FIND_DATA lpvFindData
);

[STAThread ()]
public static void Main ()
{
int context = 0;
IntPtr hInternet = InternetOpen (null,
INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
IntPtr hConnection = InternetConnect (
hInternet,
"zorkmid.dakcs.com",
INTERNET_DEFAULT_FTP_PORT,
"tom",
"vws4ever",
INTERNET_SERVICE_FTP,
0,
ref context);

WIN32_FIND_DATA findData;
IntPtr hFind = FtpFindFirstFile (
hConnection,
"*",
out findData,
INTERNET_FLAG_NEED_FILE,
out context
);
Console.WriteLine (findData.cFileName);
while (InternetFindNextFile (hFind, out findData))
{
Console.WriteLine (findData.cFileName);
}
InternetCloseHandle (hFind);
InternetCloseHandle (hConnection);
InternetCloseHandle (hInternet);


}
}
}

HTH
--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 0 Days, 4 Hours, 36 Minutes, 58 Seconds
 
T

Tom Shelton

Thanks Tom --

I've never used .net before so translating some of the code is hit and miss. Here's
my current configuration of WIN32_FIND_DATA

Public Structure WIN32_FIND_DATA
Dim dwFileAttributes As Integer
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Integer
Dim nFileSizeLow As Integer
Dim dwReserved0 As Integer
Dim dwReserved1 As Integer
<VBFixedString(260)> Public cFileName As String
<VBFixedString(14)> Public cAlternate As String
End Structure

I've also tried

Public Structure WIN32_FIND_DATA
Dim dwFileAttributes As Integer
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Integer
Dim nFileSizeLow As Integer
Dim dwReserved0 As Integer
Dim dwReserved1 As Integer
<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByVa
lTStr, SizeConst:=260)> Public cFileName As String
<VBFixedString(14),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByVa
lTStr, SizeConst:=14)> Public cAlternate As String
End Structure

Which one should I use with WinInet or is there a better way
to declare fixed length strings in structures?

For your WIN32_FIND_DATA structure, try something like:

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure WIN32_FIND_DATA
Public dwFileAttributes As Integer
Public ftCreationTime As FILETIME
Public ftLastAccessTime As FILETIME
Public ftLastWriteTime As FILETIME
Public nFileSizeHigh As Integer
Public nFileSizeLow As Integer
Public dwReserved0 As Integer
Public dwReserved1 As Integer

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=260)> _
Public cFileName As String

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=14)> _
Public cAlternate As String
End Structure


VBFixedString doesn't do anything for you in interop - it is used by the
lame native VB.NET file io methods (which are to be avoided if you care
anything about performance). Also, make sure that you use Auto in the
declaration for the functions that take this structure:

Declare Auto Function FtpFindFirstFile ....

That will make sure the CharSet is set correctly for the function as
well as the structure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top