|
-
Oct 22nd, 2003, 05:13 AM
#1
Thread Starter
Lively Member
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...
-
Oct 22nd, 2003, 06:02 AM
#2
if you know the url already....
VB Code:
[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
[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))
[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.
[/COLOR] [Color=Blue]While[/COLOR] [Color=Blue]Not[/COLOR] sReader.Peek
[Color=Blue]If[/COLOR] sReader.ReadLine = url [Color=Blue]Then
[/COLOR] Process.Start("IEXPLORE.EXE", url)
[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...
[/COLOR] [Color=Green]'///Process.Start[/COLOR] [Color=Green]([/COLOR] [Color=Green]url[/COLOR] [Color=Green])[/COLOR] [Color=Green]
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]If
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]While
[/COLOR] sReader.Close()
[Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
if you dont know the url you could try this...
VB Code:
[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
[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))
[Color=Blue]Dim[/COLOR] url [Color=Blue]As[/COLOR] [Color=Blue]String[/COLOR], strText [Color=Blue]As[/COLOR] [Color=Blue]String
[/COLOR] [Color=Blue]While[/COLOR] [Color=Blue]Not[/COLOR] sReader.Peek
strText = sReader.ReadLine
[Color=Blue]If[/COLOR] strText.StartsWith("http://") [Color=Blue]Then
[/COLOR] url = strText
Process.Start("IEXPLORE.EXE", url)
[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...
[/COLOR] [Color=Green]'///Process.Start[/COLOR] [Color=Green]([/COLOR] [Color=Green]url[/COLOR] [Color=Green])[/COLOR] [Color=Green]
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]If
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]While
[/COLOR] sReader.Close()
[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]
-
Oct 22nd, 2003, 11:47 PM
#3
Thread Starter
Lively Member
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://")
-
Oct 22nd, 2003, 11:59 PM
#4
Frenzied Member
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
-
Oct 23rd, 2003, 01:31 AM
#5
Thread Starter
Lively Member
the format of the ip address is in decimal one.. wat u mean by nested in a tag or just somewhere is the file??
-
Oct 23rd, 2003, 01:34 AM
#6
Frenzied Member
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
-
Oct 26th, 2003, 09:09 PM
#7
Thread Starter
Lively Member
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?
-
Oct 26th, 2003, 10:05 PM
#8
Frenzied Member
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
-
Oct 26th, 2003, 10:09 PM
#9
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|