Need help with File.ReadAllText

Re: Need help with File.ReadAllText

Postby ismellike » 09 Jul 2013, 18:11

You forgot one important piece of your url....

http://
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Need help with File.ReadAllText

Postby Leeizazombie » 09 Jul 2013, 18:48

wait nevermind, I fixed it lol, it had to do with already having the cmdloaded
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with File.ReadAllText

Postby Conor » 09 Jul 2013, 21:17

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.");
}
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with File.ReadAllText

Postby Leeizazombie » 09 Jul 2013, 22:09

Hmm perfect! Thanks guys!
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with File.ReadAllText

Postby Conor » 09 Jul 2013, 22:15

Leeizazombie wrote:Hmm perfect! Thanks guys!


No problem
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with File.ReadAllText

Postby Leeizazombie » 10 Jul 2013, 11:45

Oh one more thing, is there a possible way to use something like File.ReadAllLines in the webclient version?
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with File.ReadAllText

Postby joppiesaus » 10 Jul 2013, 18:46

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.
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Need help with File.ReadAllText

Postby Conor » 11 Jul 2013, 19:26

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.");
   }
}
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with File.ReadAllText

Postby AFK_Games » 17 Jul 2014, 14:14

The final code I put together for you nerds, :D

Spoiler:
AFK_Games
 
Posts: 21
Joined: 11 Apr 2014, 21:19

Re: Need help with File.ReadAllText

Postby _Retaliate_ » 17 Jul 2014, 23:14

lol, wtf? this post was made more than a year ago, why are you necroposting?
Image
_Retaliate_
 
Posts: 68
Joined: 26 Sep 2013, 11:16

Previous

Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron