Page 2 of 2

Re: Need help with File.ReadAllText

PostPosted: 09 Jul 2013, 18:11
by ismellike
You forgot one important piece of your url....

http://

Re: Need help with File.ReadAllText

PostPosted: 09 Jul 2013, 18:48
by Leeizazombie
wait nevermind, I fixed it lol, it had to do with already having the cmdloaded

Re: Need help with File.ReadAllText

PostPosted: 09 Jul 2013, 21:17
by Conor
As ismellike suggested, it probably would be a good idea to use a try/catch statement as we're reading from online.

So just change this:
Code: Select all
string content = new System.Net.WebCient().DownloadString(URL);


To:
Code: Select all
try
{
   string content = new System.Net.WebCient().DownloadString(URL);
}
catch
{
   Player.SendMessage(p, "An error occurred retrieving server news.");
}

Re: Need help with File.ReadAllText

PostPosted: 09 Jul 2013, 22:09
by Leeizazombie
Hmm perfect! Thanks guys!

Re: Need help with File.ReadAllText

PostPosted: 09 Jul 2013, 22:15
by Conor
Leeizazombie wrote:Hmm perfect! Thanks guys!


No problem

Re: Need help with File.ReadAllText

PostPosted: 10 Jul 2013, 11:45
by Leeizazombie
Oh one more thing, is there a possible way to use something like File.ReadAllLines in the webclient version?

Re: Need help with File.ReadAllText

PostPosted: 10 Jul 2013, 18:46
by joppiesaus
Wow. This is now a usefull thread for everyone(I didn't know about File.ReadAllText)!
Anyway you could also do it with streamreader, but it will require more code so it is useless.

Re: Need help with File.ReadAllText

PostPosted: 11 Jul 2013, 19:26
by Conor
Leeizazombie wrote:Oh one more thing, is there a possible way to use something like File.ReadAllLines in the webclient version?


I am not sure about this myself. I would do something like adding an uncommon symbol in the text when I want it to be a new line. There is probably a much more simpler way but I can only think of this sorry. So I'll show you what I mean.

I would store this as my textfile online:

Hello welcome to my server$Please read the rules$You can type /rules$Also check out the /help menu


Then in my code I would do something like:
Code: Select all
public override void Use(Player p, string message)
{
   try
   {
      string content = new System.Net.WebClient().DownloadString("http://conor.com/news.txt");
     
      foreach(string line in content.Split('$'))
      {
          Player.SendMessage(p, line);
      }
   }
   catch
   {
      Player.SendMessage(p, "Error getting news.");
   }
}

Re: Need help with File.ReadAllText

PostPosted: 17 Jul 2014, 14:14
by AFK_Games
The final code I put together for you nerds, :D

Spoiler:

Re: Need help with File.ReadAllText

PostPosted: 17 Jul 2014, 23:14
by _Retaliate_
lol, wtf? this post was made more than a year ago, why are you necroposting?