473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

command line prompt

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
3 2259
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
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul Deverson | last post by:
I'm a newbie to MySQL and am really confused about when I should be using the Command Line Client and when the DOS prompt. I'm using Windows XP and I've just installed MySQL 4.1.10. I can use...
10
by: Steve | last post by:
Is there anyone with experience using the Winzip command line add-in? I can't get it to create the zip file with a space in the file name. Using: RetValue = Shell("C:\winzip\wzzip.exe...
4
by: glenn | last post by:
I keep reading all sorts of books on VS that keep telling me to click on Tools/Visual Studio Command prompt to run this program or that program. However, I do not have such a menu choice. Where is...
34
by: Roman Mashak | last post by:
Hello, All! I'm implementing simple CLI (flat model, no tree-style menu etc.). Command line looks like this: <command> <param1> <param2> ... <paramN> (where N=1..4) And idea is pretty simple: ...
9
by: Endless Story | last post by:
My last version of Python was 2.4, running smoothly on XP with path c: \Python24 - no need even to include this path in PATH; everything worked as it's supposed to at the command line. Just...
4
by: =?Utf-8?B?WUlndWNoaQ==?= | last post by:
Hi , I want to run a batch file from the c# code. Every time i run the batch file command prompt is displayed. I do not want to show this command prompt. Is there any way to suppress the...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
13
by: c3950ig | last post by:
Hi, I am python newbie and the command prompt is having an issue with python. I installed python 2.4.4 onto my windows machine, opened a command prompt window, and typed python to start the...
5
by: waltbrad | last post by:
Hi folks. I'm learning Python from the Mark Lutz Book, Programming Python 3rd edition. He seems to be able to invoke the Python interpreter from any command line prompt. C:\temp>python ...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.