By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,836 Members | 1,705 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,836 IT Pros & Developers. It's quick & easy.

command line prompt

P: n/a
Hello,
I would like to write a windows application that can get the IP
address of the computer the appliation is installed on. Then the
program should ftp a file to another computer. I have no idea how to
do this. Does anyone have any ideas about how create and run commands
using a command line prompt in vb.net.

Thanks,
Billy
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Billy,

The starting point is to create a new Console Application Project. The
result of this is a program that can be run from the command line.

Regards,
Fergus
Nov 20 '05 #2

P: n/a
In article <dd**************************@posting.google.com >, Billy Cormic wrote:
Hello,
I would like to write a windows application that can get the IP
address of the computer the appliation is installed on. Then the
program should ftp a file to another computer. I have no idea how to
do this. Does anyone have any ideas about how create and run commands
using a command line prompt in vb.net.

Thanks,
Billy


Imports System.Net

.....

MessageBox.Show(Dns.GetHostByName(Dns.GetHostName( )).AddressList(0).ToString())

The above will get the IP Address of the current machine... Now, as for
the FTP part - if your needs are simple, you can always use the WinInet
API for this. I do - and it works fairly well. Here is some C# code
that I wrote (it isn't complete since, I haven't implemented of geting
ta file, but puting a file works just fine :)

// WinInet.cs
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace FireAnt.Net.Ftp
{
/// <summary>
/// Summary description for WinInet.
/// </summary>
internal 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;

[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 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 lpdwNumberOfBytesWritten
);

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

private WinInet()
{
}
}
}

// SimpleFtp.cs
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;

namespace FireAnt.Net.Ftp
{
[Flags()]
public enum AccessMode
{
Read = WinInet.GENERIC_READ,
Write = WinInet.GENERIC_WRITE,
}

public enum TransferMode
{
Ascii = WinInet.FTP_TRANSFER_TYPE_ASCII,
Binary = WinInet.FTP_TRANSFER_TYPE_BINARY,
}

/// <summary>
/// Summary description for SimpleFTP.
/// </summary>
public sealed class SimpleFtp : IDisposable
{
private IntPtr internet;
private IntPtr connection;
private IntPtr fileHandle;
private int context;

public SimpleFtp(string host, string userName, string password)
{
internet = WinInet.InternetOpen(
null,
WinInet.INTERNET_OPEN_TYPE_DIRECT,
null,
null,
0);

if (internet == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}

connection = WinInet.InternetConnect(
this.internet,
host,
WinInet.INTERNET_DEFAULT_FTP_PORT,
userName,
password,
WinInet.INTERNET_SERVICE_FTP,
0,
ref this.context);

if (connection == IntPtr.Zero)
{
WinInet.InternetCloseHandle(this.internet);
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

~SimpleFtp()
{
this.CleanUp();
}

void IDisposable.Dispose()
{
this.CleanUp();
GC.SuppressFinalize(this);
}

public void Close()
{
((IDisposable)this).Dispose();
}

public void OpenFile(string fileName, AccessMode access, TransferMode mode)
{
this.fileHandle = WinInet.FtpOpenFile(this.connection, fileName, (int) access, (int) mode, out this.context);
if (this.fileHandle == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

public int WriteFile(string buffer)
{
byte[] bytes = new ASCIIEncoding().GetBytes(buffer);
return this.WriteFile(bytes);
}

public int WriteFile(byte[] buffer)
{
int byteCount;
if (!WinInet.InternetWriteFile(this.fileHandle, buffer, buffer.Length, out byteCount))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return byteCount;
}

public bool ReadFile(string buffer)
{
// not implemented
return false;
}

public bool ReadFile(byte[] buffer)
{
// not implemented
return false;
}

private void CleanUp()
{
if (this.fileHandle != IntPtr.Zero)
{
WinInet.InternetCloseHandle(this.fileHandle);
}

if (this.connection != IntPtr.Zero)
{
WinInet.InternetCloseHandle(this.connection);
}

if (this.internet != IntPtr.Zero)
{
WinInet.InternetCloseHandle(this.internet);
}
}
}
}

You can compile the above into a class libary project, and then call the
functions from your VB.NET app. Here is a small snipit of C# code that
uses the class to create a file on a remote server...

// connect to the ftp server
status.Text = "Connecting To FTP Server...";
SimpleFTP ftp = new SimpleFtp(
VicApp.setup.HostAddress,
VicApp.setup.UserName,
VicApp.setup.Password);

// create the file on the remote server, and open it for
// writing.
status.Text = "Opening Export File";
ftp.OpenFile("DlgXport.TXT", AccessMode.Write, TransferMode.Ascii);

StringBuilder buffer;
Dialog d;
FileInfo fi;
foreach (string fileName in fileNames)
{
buffer = new StringBuilder(40);
fi = new FileInfo(fileName);
d = Dialog.Load(VicApp.setup.DialogPath, fi.Name);
buffer.Append(d.Title).Append("|").Append(((d.Mess aging) ?
"M" : "P")).Append("\n");
ftp.WriteFile(buffer.ToString());
}

ftp.Close();

Now, if your transfering a file that already exists on your machine, the
code above would be a little different, but still similar. You would
just create your buffer by opening the file and reading from it :)

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3

P: n/a
* bi**********@hotmail.com (Billy Cormic) scripsit:
I would like to write a windows application that can get the IP
address of the computer the appliation is installed on. Then the
program should ftp a file to another computer. I have no idea how to
do this. Does anyone have any ideas about how create and run commands
using a command line prompt in vb.net.


See:

<http://www.mvps.org/dotnet/dotnet/samples/miscsamples/downloads/RedirectConsole.zip>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.