473,385 Members | 1,372 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.

Read Unix file from VB.Net?

How can I read a text file that is on a UNIX server in VB.Net?

Please keep it simple.

Thanks,
John
Nov 21 '05 #1
9 7863
"John Howard" <jo*********@dcf.state.fl.us> schrieb:
How can I read a text file that is on a UNIX server in VB.Net?


What's the exact problem? Accessing the server? Provide more information
about the server and the connection to the server/network if you want to do
this. Or do you have problems to read the file using the right encoding?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
I don't know how to build the connection string. What provider should I
use? How do I build a data source when the file does not have a local drive
letter?

Thanks,
John

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u0**************@TK2MSFTNGP10.phx.gbl...
"John Howard" <jo*********@dcf.state.fl.us> schrieb:
How can I read a text file that is on a UNIX server in VB.Net?
What's the exact problem? Accessing the server? Provide more information
about the server and the connection to the server/network if you want to

do this. Or do you have problems to read the file using the right encoding?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
John,

Because of your answer I thought that Herfried was again showing his
connection string message in the wrong place, however he asked you in my
opinion the right questions.

But in the first place is this not a VBNet question, more a networking
question.

You should make a drive accesable, and than you can do it with VBNet. Or you
should talk about by instance an Oracle (or whatever) database on Unix

:-)

Cor.

"John Howard" <jo*********@dcf.state.fl.us>
use? How do I build a data source when the file does not have a local
drive
letter?

Thanks,
John

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u0**************@TK2MSFTNGP10.phx.gbl...
"John Howard" <jo*********@dcf.state.fl.us> schrieb:
> How can I read a text file that is on a UNIX server in VB.Net?


What's the exact problem? Accessing the server? Provide more
information
about the server and the connection to the server/network if you want to

do
this. Or do you have problems to read the file using the right encoding?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #4
My mistake. I was thinking about DB access instead of file access. Please
disregard my preveious reply.

I am attempting to access the UNIX file "xyz.txt" in directory "usercount"
on server "dev1". When I go to Windows Explorer and click on dev1,
"ftp://dev1/usercount/" appears on the address line. If I try to read file
"ftp://dev1/usercount/xyz.txt" in my program, I get "The given path's format
is not supported.". Any ideas?
Thanks again,
John

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u0**************@TK2MSFTNGP10.phx.gbl...
"John Howard" <jo*********@dcf.state.fl.us> schrieb:
How can I read a text file that is on a UNIX server in VB.Net?
What's the exact problem? Accessing the server? Provide more information
about the server and the connection to the server/network if you want to

do this. Or do you have problems to read the file using the right encoding?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
"John Howard" <jo*********@dcf.state.fl.us> wrote in news:41a4b0f1$1
@news.hcs.net:
How can I read a text file that is on a UNIX server in VB.Net?


Depends on what method of access you have to the Unix server.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #6
You need to understand what is going on when you put that ftp:// address in
Windows Explorer. It's not just "Accessing a file from UNIX". You are not
accessing a local or "mounted" file system.

In the background, Windows Explorer is using an FTP client to connect to
your server's FTP server, which means logging in, listing files, doing a GET
or PUT on the file, etc.

You need to build an FTP client that will go and fetch the document. It's
not a matter of simple file access.

"John Howard" <jo*********@dcf.state.fl.us> wrote in message
news:41********@news.hcs.net...
My mistake. I was thinking about DB access instead of file access.
Please
disregard my preveious reply.

I am attempting to access the UNIX file "xyz.txt" in directory "usercount"
on server "dev1". When I go to Windows Explorer and click on dev1,
"ftp://dev1/usercount/" appears on the address line. If I try to read
file
"ftp://dev1/usercount/xyz.txt" in my program, I get "The given path's
format
is not supported.". Any ideas?
Thanks again,
John

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u0**************@TK2MSFTNGP10.phx.gbl...
"John Howard" <jo*********@dcf.state.fl.us> schrieb:
> How can I read a text file that is on a UNIX server in VB.Net?


What's the exact problem? Accessing the server? Provide more
information
about the server and the connection to the server/network if you want to

do
this. Or do you have problems to read the file using the right encoding?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #7
"John Howard" <jo*********@dcf.state.fl.us> wrote in
news:41********@news.hcs.net:
If I try to read file
"ftp://dev1/usercount/xyz.txt" in my program, I get "The given path's
format is not supported.". Any ideas?


You need an FTP client to access the server.

http://www.xceedsoft.com/ has a good FTP component (not free though).
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #8
"Lucas Tam" <RE********@rogers.com> schrieb:
If I try to read file
"ftp://dev1/usercount/xyz.txt" in my program, I get "The given path's
format is not supported.". Any ideas?


You need an FTP client to access the server.

http://www.xceedsoft.com/ has a good FTP component (not free though).


Accessing FTP servers in .NET applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=ftp&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9
In article <41********@news.hcs.net>, John Howard wrote:
How can I read a text file that is on a UNIX server in VB.Net?

Please keep it simple.

Thanks,
John


// 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;

private const int BUFFER_SIZE = 2048;

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 string CurrentDirectory
{
get
{
StringBuilder path = new StringBuilder(260);
int bufferSize = path.Capacity;

if (!WinInet.FtpGetCurrentDirectory(this.connection,
path, ref bufferSize))
{
throw new
Win32Exception(Marshal.GetLastWin32Error());
}
return path.ToString();
}
set
{
if (!WinInet.FtpSetCurrentDirectory(this.connection,
value))
{
throw new
Win32Exception(Marshal.GetLastWin32Error());
}
}
}

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 void CloseFile()
{
if (this.fileHandle != IntPtr.Zero)
{
if (WinInet.InternetCloseHandle(this.fileHandle))
{
this.fileHandle = IntPtr.Zero;
}
else
{
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(out string buffer)
{
// clear the buffer...
buffer = string.Empty;

// read from the file
int bytesRead;
byte[] readBuffer = new byte[SimpleFtp.BUFFER_SIZE];
bool success = WinInet.InternetReadFile(this.fileHandle,
readBuffer, readBuffer.Length, out bytesRead);

// the call failed!
if (!success)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}

// we got some data, so convert it for the return...
if (bytesRead != 0)
{
buffer = Encoding.ASCII.GetString(readBuffer, 0,
bytesRead);
}

return (bytesRead != 0) ? true : false;
}

public bool ReadFile(byte[] buffer)
{
int bytesRead;
bool success = WinInet.InternetReadFile(this.fileHandle,
buffer, buffer.Length, out bytesRead);
if (!success)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return (bytesRead != 0) ? true : false;
}

public void GetFile(string localFile, string remoteFile, bool
fail)
{
GetFile(localFile, remoteFile, TransferMode.Ascii, fail);
}

public void GetFile(string localFile, string remoteFile,
TransferMode mode, bool fail)
{
if(!WinInet.FtpGetFile(connection, remoteFile, localFile,
fail, 0, (int)mode, out context))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

public void PutFile(string localFile, string remoteFile)
{
this.PutFile(localFile, remoteFile, TransferMode.Ascii);
}

public void PutFile(string localFile, string remoteFile,
TransferMode mode)
{
if (!WinInet.FtpPutFile(this.connection, localFile,
remoteFile, (int) mode, out this.context))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

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);
}
}
}
}

// 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 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);

private WinInet()
{
}
}
}

This is some code I wrote a while back, which I have used with great
success for exactly what your asking... I know it's C#, but if you
compile it into it's own dll, then you should be able to use it from
VB.NET...

Dim ftp As New SimpleFtp ("yourhost", "youruser", "yourpassword")
Dim buffer As String

ftp.OpenFile ("YourFile.Txt", AccessMode.Read, TransferMode.Ascii)

Do while (ftp.ReadFile (buffer))
' Do stuff with the buffer
loop

ftp.CloseFile ()
ftp.Close ()

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 33 Days, 0 Hours, 3 Minutes, 47 Seconds
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: sea | last post by:
I have text files in the following format: 123,34, ,345,890, 123,23 .. .. .. As you can see, the problem is that (1) the commas can occur in
3
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
2
by: Stan Sainte-Rose | last post by:
Hi, I need to read a kind of text file from vb.net I do know how to read a simple file, but this one seems to have special characters. Some characters appears like squares when open this file...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
7
by: Marcus Kwok | last post by:
I am working on a program that reads and processes large text files (on the order of 32 MB, so not too huge), so I wanted to add a progress indicator so I can estimate when it will finish. I just...
23
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
16
by: mazwolfe | last post by:
Someone recently asked about reading lines. I had this code written some time ago (part of a BASIC-style interpreter based on H. Shildts in Art of C) to read a file with the lines ended in any...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.