Read Unix file from VB.Net?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Howard

    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


  • Herfried K. Wagner [MVP]

    #2
    Re: Read Unix file from VB.Net?

    "John Howard" <john_howard@dc f.state.fl.us> schrieb:[color=blue]
    > How can I read a text file that is on a UNIX server in VB.Net?[/color]

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

    Comment

    • John Howard

      #3
      Re: Read Unix file from VB.Net?

      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]" <hirf-spam-me-here@gmx.at> wrote in message
      news:u0Ku6mk0EH A.3832@TK2MSFTN GP10.phx.gbl...[color=blue]
      > "John Howard" <john_howard@dc f.state.fl.us> schrieb:[color=green]
      > > How can I read a text file that is on a UNIX server in VB.Net?[/color]
      >
      > 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[/color]
      do[color=blue]
      > 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/>
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Read Unix file from VB.Net?

        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" <john_howard@dc f.state.fl.us>
        [color=blue]
        > use? How do I build a data source when the file does not have a local
        > drive
        > letter?
        >
        > Thanks,
        > John
        >
        > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        > news:u0Ku6mk0EH A.3832@TK2MSFTN GP10.phx.gbl...[color=green]
        >> "John Howard" <john_howard@dc f.state.fl.us> schrieb:[color=darkred]
        >> > How can I read a text file that is on a UNIX server in VB.Net?[/color]
        >>
        >> 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[/color]
        > do[color=green]
        >> 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/>
        >>[/color]
        >
        >[/color]


        Comment

        • John Howard

          #5
          Re: Read Unix file from VB.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]" <hirf-spam-me-here@gmx.at> wrote in message
          news:u0Ku6mk0EH A.3832@TK2MSFTN GP10.phx.gbl...[color=blue]
          > "John Howard" <john_howard@dc f.state.fl.us> schrieb:[color=green]
          > > How can I read a text file that is on a UNIX server in VB.Net?[/color]
          >
          > 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[/color]
          do[color=blue]
          > 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/>
          >[/color]


          Comment

          • Lucas Tam

            #6
            Re: Read Unix file from VB.Net?

            "John Howard" <john_howard@dc f.state.fl.us> wrote in news:41a4b0f1$1
            @news.hcs.net:
            [color=blue]
            > How can I read a text file that is on a UNIX server in VB.Net?
            >[/color]

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

            --
            Lucas Tam (REMOVEnntp@rog ers.com)
            Please delete "REMOVE" from the e-mail address when replying.

            Comment

            • Zeno Lee

              #7
              Re: Read Unix file from VB.Net?

              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" <john_howard@dc f.state.fl.us> wrote in message
              news:41a4cc42$1 @news.hcs.net.. .[color=blue]
              > 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]" <hirf-spam-me-here@gmx.at> wrote in message
              > news:u0Ku6mk0EH A.3832@TK2MSFTN GP10.phx.gbl...[color=green]
              >> "John Howard" <john_howard@dc f.state.fl.us> schrieb:[color=darkred]
              >> > How can I read a text file that is on a UNIX server in VB.Net?[/color]
              >>
              >> 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[/color]
              > do[color=green]
              >> 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/>
              >>[/color]
              >
              >[/color]


              Comment

              • Lucas Tam

                #8
                Re: Read Unix file from VB.Net?

                "John Howard" <john_howard@dc f.state.fl.us> wrote in
                news:41a4cc42$1 @news.hcs.net:
                [color=blue]
                > 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?[/color]

                You need an FTP client to access the server.

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


                --
                Lucas Tam (REMOVEnntp@rog ers.com)
                Please delete "REMOVE" from the e-mail address when replying.

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Read Unix file from VB.Net?

                  "Lucas Tam" <REMOVEnntp@rog ers.com> schrieb:[color=blue][color=green]
                  >> 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?[/color]
                  >
                  > You need an FTP client to access the server.
                  >
                  > http://www.xceedsoft.com/ has a good FTP component (not free though).[/color]

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

                  Comment

                  • Tom Shelton

                    #10
                    Re: Read Unix file from VB.Net?

                    In article <41a4b0f1$1@new s.hcs.net>, John Howard wrote:[color=blue]
                    > How can I read a text file that is on a UNIX server in VB.Net?
                    >
                    > Please keep it simple.
                    >
                    > Thanks,
                    > John
                    >
                    >[/color]

                    // SimpleFtp.cs
                    using System;
                    using System.Componen tModel;
                    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_TRA NSFER_TYPE_ASCI I,
                    Binary = WinInet.FTP_TRA NSFER_TYPE_BINA RY,
                    }

                    /// <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(strin g host, string userName, string password)
                    {
                    internet = WinInet.Interne tOpen(
                    null,
                    WinInet.INTERNE T_OPEN_TYPE_DIR ECT,
                    null,
                    null,
                    0);

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

                    connection = WinInet.Interne tConnect(
                    this.internet,
                    host,
                    WinInet.INTERNE T_DEFAULT_FTP_P ORT,
                    userName,
                    password,
                    WinInet.INTERNE T_SERVICE_FTP,
                    0,
                    ref this.context);

                    if (connection == IntPtr.Zero)
                    {
                    WinInet.Interne tCloseHandle(th is.internet);
                    throw new Win32Exception( Marshal.GetLast Win32Error());
                    }
                    }

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

                    void IDisposable.Dis pose()
                    {
                    this.CleanUp();
                    GC.SuppressFina lize(this);
                    }

                    public string CurrentDirector y
                    {
                    get
                    {
                    StringBuilder path = new StringBuilder(2 60);
                    int bufferSize = path.Capacity;

                    if (!WinInet.FtpGe tCurrentDirecto ry(this.connect ion,
                    path, ref bufferSize))
                    {
                    throw new
                    Win32Exception( Marshal.GetLast Win32Error());
                    }
                    return path.ToString() ;
                    }
                    set
                    {
                    if (!WinInet.FtpSe tCurrentDirecto ry(this.connect ion,
                    value))
                    {
                    throw new
                    Win32Exception( Marshal.GetLast Win32Error());
                    }
                    }
                    }

                    public void Close()
                    {
                    ((IDisposable)t his).Dispose();
                    }

                    public void OpenFile(string fileName, AccessMode access,
                    TransferMode mode)
                    {
                    this.fileHandle = WinInet.FtpOpen File(this.conne ction,
                    fileName, (int) access, (int) mode, out this.context);
                    if (this.fileHandl e == IntPtr.Zero)
                    {
                    throw new Win32Exception( Marshal.GetLast Win32Error());
                    }
                    }

                    public void CloseFile()
                    {
                    if (this.fileHandl e != IntPtr.Zero)
                    {
                    if (WinInet.Intern etCloseHandle(t his.fileHandle) )
                    {
                    this.fileHandle = IntPtr.Zero;
                    }
                    else
                    {
                    throw new
                    Win32Exception( Marshal.GetLast Win32Error());
                    }
                    }
                    }

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

                    public int WriteFile(byte[] buffer)
                    {
                    int byteCount;
                    if (!WinInet.Inter netWriteFile(th is.fileHandle, buffer,
                    buffer.Length, out byteCount))
                    {
                    throw new Win32Exception( Marshal.GetLast Win32Error());
                    }
                    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.BUFFE R_SIZE];
                    bool success = WinInet.Interne tReadFile(this. fileHandle,
                    readBuffer, readBuffer.Leng th, out bytesRead);

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

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

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

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

                    public void GetFile(string localFile, string remoteFile, bool
                    fail)
                    {
                    GetFile(localFi le, remoteFile, TransferMode.As cii, fail);
                    }

                    public void GetFile(string localFile, string remoteFile,
                    TransferMode mode, bool fail)
                    {
                    if(!WinInet.Ftp GetFile(connect ion, remoteFile, localFile,
                    fail, 0, (int)mode, out context))
                    {
                    throw new Win32Exception( Marshal.GetLast Win32Error());
                    }
                    }

                    public void PutFile(string localFile, string remoteFile)
                    {
                    this.PutFile(lo calFile, remoteFile, TransferMode.As cii);
                    }

                    public void PutFile(string localFile, string remoteFile,
                    TransferMode mode)
                    {
                    if (!WinInet.FtpPu tFile(this.conn ection, localFile,
                    remoteFile, (int) mode, out this.context))
                    {
                    throw new Win32Exception( Marshal.GetLast Win32Error());
                    }
                    }

                    private void CleanUp()
                    {
                    if (this.fileHandl e != IntPtr.Zero)
                    {
                    WinInet.Interne tCloseHandle(th is.fileHandle);
                    }

                    if (this.connectio n != IntPtr.Zero)
                    {
                    WinInet.Interne tCloseHandle(th is.connection);
                    }

                    if (this.internet != IntPtr.Zero)
                    {
                    WinInet.Interne tCloseHandle(th is.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_T YPE_PRECONFIG = 0;
                    public const int INTERNET_OPEN_T YPE_DIRECT = 1;
                    public const int INTERNET_OPEN_T YPE_PROXY = 3;
                    public const short INTERNET_DEFAUL T_FTP_PORT = 21;
                    public const int INTERNET_SERVIC E_FTP = 1;
                    public const int FTP_TRANSFER_TY PE_ASCII = 0x01;
                    public const int FTP_TRANSFER_TY PE_BINARY = 0x02;
                    public const int GENERIC_WRITE = 0x40000000;
                    public const int GENERIC_READ = unchecked((int) 0x80000000);
                    public const int MAX_PATH = 260;

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

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

                    [DllImport("wini net.dll", CharSet=CharSet .Auto,
                    SetLastError=tr ue)]
                    public static extern bool FtpGetCurrentDi rectory(
                    IntPtr hConnect,
                    StringBuilder lpszCurrentDire ctory,
                    ref int lpdwCurrentDire ctory);

                    [DllImport("wini net.dll", CharSet=CharSet .Auto,
                    SetLastError=tr ue)]
                    public static extern bool FtpSetCurrentDi rectory(
                    IntPtr hConnect,
                    string lpszCurrentDire ctory);

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

                    [DllImport("wini net.dll", SetLastError=tr ue)]
                    public static extern bool InternetWriteFi le(
                    IntPtr hFile,
                    [MarshalAs(Unman agedType.LPArra y)] byte[] lpBuffer,
                    int dwNumberOfBytes ToWrite,
                    out int lpdwNumberOfByt esWritten);

                    [DllImport("wini net.dll", SetLastError=tr ue)]
                    public static extern bool InternetReadFil e(
                    IntPtr hFile,
                    [MarshalAs(Unman agedType.LPArra y)] byte[] lpBuffer,
                    int dwNumberOfBytes ToRead,
                    out int lpdwNumberOfByt esRead
                    );

                    [DllImport("wini net.dll", CharSet=CharSet .Auto,
                    SetLastError=tr ue)]
                    public static extern bool InternetCloseHa ndle(IntPtr hInternet);

                    [DllImport("wini net.dll", CharSet=CharSet .Auto,
                    SetLastError=tr ue)]
                    public static extern bool FtpPutFile (
                    IntPtr hConnect,
                    string lpszLocalFile,
                    string lpszNewRemoteFi le,
                    int dwFlags,
                    out int dwContext);

                    [DllImport("wini net.dll", CharSet=CharSet .Auto,
                    SetLastError=tr ue)]
                    public static extern bool FtpGetFile (
                    IntPtr hConnect,
                    string lpszRemoteFile,
                    string lpszLocalFile,
                    bool failIfExists,
                    int dwFlagsAttribut es,
                    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", "yourpasswo rd")
                    Dim buffer As String

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

                    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

                    Comment

                    Working...