Read A File Through Internet

  • Thread starter Thread starter noricat
  • Start date Start date
N

noricat

Guest
Dear all,

I am new to Vb. Now i am finding a method to read a file on server through internet by VB.net. Just like OPEN(file, ">file.txt"); by perl. How can I do like this? Use system.net? Can anybody give me some help. Thanks a million.

NoriCat
 
You can use the WebClient class in the System.Net namespace to
download files and read streams from files.

Use the DownloadFile method to download a file (duh :)).
Use the OpenRead method to open the remote file as a stream,
which you can then read into a string by creating a StreamReader
and then ReadToEnd.

Heres an example of downloading a remote file to a string:
Code:
Dim sData As String
Dim stream As IO.StreamReader
Dm wc As New Net.WebClient()

stream = New IO.StreamReader(wc.OpenRead("file URL here"))

sData = stream.ReadToEnd

I use the above method all the time when downloading HTML to
parse through it.

HTH
 
If I want to delete that file, what class and method should I use? :confused:
I tried IO.File.delete, but failed :(
It seems must connected to the server first.:confused:
 
File.Delete should work, it would help if you told us what error that gives you.
 
Delete what file? If youre using the code example I mentioned,
there is no file created; a stream is created to read from, which is
only temporary.
 
Delete the file "file.txt". I read the file successfully by using your method StreamReader [(wc.OpenRead("http://www.xxx.com/file.txt"))] :). After read, I want to delete it immediately. I think my program should access the server so can receive the permission to delete the file. But what class and method should I use. I search for a few days...but still didnt find it.:( :( :(
 
Its not going to be that easy to delete a file from a webserver!

Youll have to go in via FTP, probably.
 
Does anyone know how to accomplish this same task with VB 6 ?
Just to reiterate, I need to grab the title of a Internet based web page.
I really do not wish to download the entire file, I would love to "open a stream to it", I saw the example but do not recall anything like that in VB 6.

I have VS.net, but have not installed it yet, I would probably be lost in it and would prefer to stick to vs6 at this time.

Any help much appreciated !:)
 
I hate to bring up a dead topic, but Im a newbie to VB.Net and was wondering, once youve got the string, howd I go about reading the contents line by line, like an ini file, for example:

[User]
SomeInfoHere

[Pass]
SomeMoreInfoHere

Howd I be able to read [User] as a value, being "SomeInfoHere" and [Pass] as "SomeMoreInfoHere " ? Thanks...
 
You wouldnt read it line-by-line, you will read the entire content using ReadToEnd, then you will need to parse whatever the strings value is yourself.
 
Look into class Regex or do a char by char search with a loop.
C#:
string word="[House]";
bool match=false;
int matchIndex = -1;
for(int i=0; i < MySearchDocument.Length; i++)
{
     if ( MySearchDocument[i] == "[" )
     {
         for(int h=1; h < word.Length; h++)
          {
               if(word[i]==MySearchDocument[i+h])
                {
                  match=true;
                }
               else match=false;
          }
      }
       if(match)
       {
           //You have what you were looking for.
           break;  //Break out of the nested loop.
       }
}
I did not test this but I hope you get the idea. If you cannot read C# at all there are some translators on the internet that allow you to paste code in a text box to see it in VB.
 
Last edited by a moderator:
Back
Top