Results 1 to 9 of 9

Thread: Reading textfile

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68

    Reading textfile

    Hi, i got a textfile contain an IP address. Then i need to read iyt and pass into the application to able to run my application. How do i read from a textfile using vb.net? Can u pls show mi some example codes? Thanks all...

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    if you know the url already....
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button1_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button1.Click
    2.  
    3.         [Color=Blue]Dim[/COLOR] sReader [Color=Blue]As[/COLOR] [Color=Blue]New[/COLOR] StreamReader([Color=Blue]New[/COLOR] FileStream("C:\your_textfile_path.txt", FileMode.Open))
    4.  
    5.         [Color=Blue]Dim[/COLOR] url [Color=Blue]As[/COLOR] [Color=Blue]String[/COLOR] = "http://google.com" [Color=Green]'///[/COLOR] [Color=Green]your[/COLOR] [Color=Green]link[/COLOR] [Color=Green]to[/COLOR] [Color=Green]use[/COLOR] [Color=Green]from[/COLOR] [Color=Green]in[/COLOR] [Color=Green]the[/COLOR] [Color=Green]textfile.
    6. [/COLOR]        [Color=Blue]While[/COLOR] [Color=Blue]Not[/COLOR] sReader.Peek
    7.             [Color=Blue]If[/COLOR] sReader.ReadLine = url [Color=Blue]Then
    8. [/COLOR]                Process.Start("IEXPLORE.EXE", url)
    9.                 [Color=Green]'///[/COLOR] [Color=Green]or[/COLOR] [Color=Green]to[/COLOR] [Color=Green]use[/COLOR] [Color=Green]the[/COLOR] [Color=Green]default[/COLOR] [Color=Green]browser[/COLOR] [Color=Green]window[/COLOR] [Color=Green]rather[/COLOR] [Color=Green]than[/COLOR] [Color=Green]internet[/COLOR] [Color=Green]explorer...
    10. [/COLOR]                [Color=Green]'///Process.Start[/COLOR] [Color=Green]([/COLOR] [Color=Green]url[/COLOR] [Color=Green])[/COLOR] [Color=Green]
    11. [/COLOR]            [Color=Blue]End[/COLOR] [Color=Blue]If
    12. [/COLOR]        [Color=Blue]End[/COLOR] [Color=Blue]While
    13. [/COLOR]        sReader.Close()
    14.  
    15.     [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    if you dont know the url you could try this...
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button2_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button2.Click
    2.  
    3.         [Color=Blue]Dim[/COLOR] sReader [Color=Blue]As[/COLOR] [Color=Blue]New[/COLOR] StreamReader([Color=Blue]New[/COLOR] FileStream("C:\your_textfile_path.txt", FileMode.Open))
    4.  
    5.         [Color=Blue]Dim[/COLOR] url [Color=Blue]As[/COLOR] [Color=Blue]String[/COLOR], strText [Color=Blue]As[/COLOR] [Color=Blue]String
    6. [/COLOR]        [Color=Blue]While[/COLOR] [Color=Blue]Not[/COLOR] sReader.Peek
    7.             strText = sReader.ReadLine
    8.             [Color=Blue]If[/COLOR] strText.StartsWith("http://") [Color=Blue]Then
    9. [/COLOR]                url = strText
    10.                 Process.Start("IEXPLORE.EXE", url)
    11.                 [Color=Green]'///[/COLOR] [Color=Green]or[/COLOR] [Color=Green]to[/COLOR] [Color=Green]use[/COLOR] [Color=Green]the[/COLOR] [Color=Green]default[/COLOR] [Color=Green]browser[/COLOR] [Color=Green]window[/COLOR] [Color=Green]rather[/COLOR] [Color=Green]than[/COLOR] [Color=Green]internet[/COLOR] [Color=Green]explorer...
    12. [/COLOR]                [Color=Green]'///Process.Start[/COLOR] [Color=Green]([/COLOR] [Color=Green]url[/COLOR] [Color=Green])[/COLOR] [Color=Green]
    13. [/COLOR]            [Color=Blue]End[/COLOR] [Color=Blue]If
    14. [/COLOR]        [Color=Blue]End[/COLOR] [Color=Blue]While
    15. [/COLOR]        sReader.Close()
    16.  
    17.     [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    If i am using a data adapter, how do i change e connection by reading the text file?
    Y does ur code got this line? i not using web application one.
    strText.StartsWith("http://")

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Originally posted by berrie_luv
    If i am using a data adapter, how do i change e connection by reading the text file?
    Y does ur code got this line? i not using web application one.
    strText.StartsWith("http://")
    It has nothing to do with web or windows application. It just searches for strings that start with "http://"
    If I understood you well, you are searching for a way to extract an IP address from a text file and pass it to your connection string. Is that right? Do you know the format of IP in the text, is it going to be decimal or binary? Is it nested in a tag or just somewhere is the file?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    the format of the ip address is in decimal one.. wat u mean by nested in a tag or just somewhere is the file??

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    For example your file may read:
    <ip>192.168.1.1</ip>
    or maybe
    IP Address=192.168.1.1

    or non of the aboves and just somewhere in the text there is 192.168.1.1. just like the previous sentence.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    Hi, if i wan save e call text file (ip address) in a form, then the rest of my forms will call my ip address form 1st before going in each of the various form functions, how to do ah?

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You want the contents of the file be available to all of your forms? You can keep it in a module or class level variable. But if the data is large, you must be careful of the resources it may require.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    Ok, i save it in a class. Then how to call e class in my form? how to import e class to my form?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width