Need help with File.ReadAllText

Need help with File.ReadAllText

Postby Leeizazombie » 08 Jul 2013, 11:51

When I use this code:
Code: Select all
   {
            string news = File.ReadAllText("www.leestorage.99k.org/news.txt");
            Player.SendMessage(p, news);
      }


It shows the following error:
Code: Select all

-------------------------

Error CS0103
Message: The name 'File' does not exist in the current context
Line: 37


Is there any other way to make the chat show a text from the internet?
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 ismellike » 08 Jul 2013, 15:21

You have to add the namespace, "using System.IO;".
That won't work for websites though.
I would recommend that you try to read some stuff about webclients from System.Net.

You can see this article for more. [Here]
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 » 08 Jul 2013, 15:52

I did use that name space, and even with a .File at the end, and still wont work, but okay thanks.
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, 02:02

Leeizazombie wrote:I did use that name space, and even with a .File at the end, and still wont work, but okay thanks.


It's quite strange that it won't work for you if you are adding the library at the top.

You can try to use it directly in the code like this
Code: Select all
string news = System.IO.File.ReadAllText("filepath");


Also, what you're trying to do is slightly wrong. The File.ReadAllText method allows you to read text from a file stored at a specific path on your computer. To read from an online text file like you're trying to do, you would do the following:

Code: Select all
string URL = "www.yourwebsite.com/file.txt";

string content = new System.Net.WebClient().DownloadString(URL);
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, 12:06

So when I use the following:

Code: Select all
/*
   Auto-generated command skeleton class.

   Use this as a basis for custom commands implemented via the MCDzienny scripting framework.
   File and class should be named a specific way.  For example, /update is named 'CmdUpdate.cs' for the file, and 'CmdUpdate' for the class.
*/

// Add any other using statements you need up here, of course.
// As a note, MCDzienny is designed for .NET 3.5.
using System;
using System.IO.File;

namespace MCDzienny
{
   public class CmdNews1 : Command
   {
      // The command's name, in all lowercase.  What you'll be putting behind the slash when using it.
      public override string name { get { return "news"; } }

      // Command's shortcut (please take care not to use an existing one, or you may have issues.
      public override string shortcut { get { return ""; } }

      // Determines which submenu the command displays in under /help.
      public override string type { get { return "other"; } }

      // Determines whether or not this command can be used in a museum.  Block/map altering commands should be made false to avoid errors.
      public override bool museumUsable { get { return false; } }

      // Determines the command's default rank.  Valid values are:
      // LevelPermission.Nobody, LevelPermission.Banned, LevelPermission.Guest
      // LevelPermission.Builder, LevelPermission.AdvBuilder, LevelPermission.Operator, LevelPermission.Admin
      public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

      // This is where the magic happens, naturally.
      // p is the player object for the player executing the command.  message is everything after the command invocation itself.
      public override void Use(Player p, string message)
      {
            string URL = "www.leestorage.99k.org/news.txt";

            string content = new System.Net.WebClient().DownloadString(URL);

      }

      // This one controls what happens when you use /help [commandname].
      public override void Help(Player p)
           
      {
         Player.SendMessage(p, "/news1 - Does stuff.  Example command.");
      }
   }
}


It gives me the following compilng error:

Code: Select all

-------------------------

Error CS0138
Message: A using namespace directive can only be applied to namespaces; 'System.IO.File' is a type not a namespace
Line: 11
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, 15:39

You only need to add the following;
Code: Select all
using System.IO;


Then you can use the 'File' class from this library, simply like;
Code: Select all
File.WriteAllText("path", "content");


However, for your command - which reads from an online text file, you will not need this library anyway. The library you need is System.Net. However, I used it directly in the code when I gave you that example, so you do not need to reference this library at the top.

So, I'll code it for you to look at :) If you were to read from an online text file, you could do this.
Code: Select all
using System;

namespace MCDzienny
{
   public class CmdNews : Command
   {
      public override string name { get { return "news"; } }
      public override string shortcut { get { return ""; } }
      public override string type { get { return "information"; } }
      public override bool museumUsable { get { return true; } }
      public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

      public override void Use(Player p, string message)
      {
            string URL = "www.leestorage.99k.org/news.txt";
            string content = new System.Net.WebClient().DownloadString(URL);

            Player.SendMessage(p, "Current server news:");
            Player.SendMessage(p, content);
      }

      public override void Help(Player p)       
      {
         Player.SendMessage(p, "/news - View the server news");
      }
   }
}


More reliably, you probably want to just store your news in a text file on your PC. So, you could do the following.
Code: Select all
using System;
using System.IO;

namespace MCDzienny
{
   public class CmdNews : Command
   {
      public override string name { get { return "news"; } }
      public override string shortcut { get { return ""; } }
      public override string type { get { return "information"; } }
      public override bool museumUsable { get { return true; } }
      public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

      private static string newsPath = "extra/news.txt";

      public override void Init()
      {
           if (!File.Exists(newsPath))
           {
               File.WriteAllText(newsPath, "No news.");
           }
      }

      public override void Use(Player p, string message)
      {
            string content = File.ReadAllText(newsPath);

            Player.SendMessage(p, "Current server news:");
            Player.SendMessage(p, content);
      }

      public override void Help(Player p)       
      {
         Player.SendMessage(p, "/news - View the server news");
      }
   }
}


You can also display a better looking news feed. You can use the File.ReadAllLines method and a foreach loop, to display lines specifically. I'll let you try and work this out yourself if you fancy a challenge :)
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, 16:52

Thank you so much, I specifically have it on my website for my server is being hosted for me, so I send the hoster my command and I can edit the .txt any time :)
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 Leeizazombie » 09 Jul 2013, 16:58

I'm not sure why but It keeps saying:

Code: Select all
(16:56:32) CONSOLE: Failed command.


I also tried without Console (this is a test server, unported)
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 ismellike » 09 Jul 2013, 17:06

First, tell us which one you are using.
You can try surrounding the inside of the Use method with this

Code: Select all
try
{
    //the inside of use method
}
catch(Exception ex)
{
    Server.s.Log(ex.ToString(),false);
}
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, 17:14

i used:

Code: Select all
using System;

namespace MCDzienny
{
    public class Cmdnews1 : Command
    {
        public override string name { get { return "news"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "information"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

        public override void Use(Player p, string message)
        {
            string URL = "www.leestorage.99k.org/news.txt";
            string content = new System.Net.WebClient().DownloadString(URL);

            Player.SendMessage(p, "Current server news:");
            Player.SendMessage(p, content);
        }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/news - View the server news");
        }
    }
}
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.

Next

Return to Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron