【问题标题】:Load text file from url into an array将文本文件从 url 加载到数组中
【发布时间】:2013-11-26 15:18:41
【问题描述】:

我正在使用

string[] defaultcommands = (File.ReadAllLines(@"commands"));

将解决方案中的本地文件内容作为数组加载。我希望能够从我的工作站更新该文件的内容,并在所有正在运行的实例上生效。为此,我想从如下网址加载默认命令文件:https://mywebsite.com/assistant/commands.txt。我试过 string[] defaultcommands = (File.ReadAllLines(@"https://mywebsite.com/assistant/commands.txt"));但它不明白如何处理我猜的 URL。它返回:System.NotSupportedException。

如何将文本文件从 URL 加载到数组中?

【问题讨论】:

    标签: c# arrays url


    【解决方案1】:

    看看WebClient 类。

    它给出了这个例子:

    WebClient client = new WebClient ();
    string reply = client.DownloadString (address);
    
    Console.WriteLine (reply);
    

    【讨论】:

    • 我得到“找不到类型或命名空间 WebClient。”此外,“字段初始化程序不能引用非静态字段、方法或属性 'assistant.Form1.client。”
    • System.Net。您是否添加了该命名空间和引用?
    • 我已经说过“使用 System.Net;”在文档的开头。接下来,在我的命名空间“AssistantOne”下,我有“public partial class Form1:Form”然后我把你的“WebClient client = new WebClient(); string reply = client.DownloadString("mysite.com/commands.txt);”但是它给出了一个错误声明客户端“字段初始值设定项无法引用非静态字段、方法或属性“AssistantOne.Form1.client。”
    • 我还得到“无法将类型 'void' 隐式转换为 'string[]'
    • 是的,这里是整个项目文件夹。我已经使用了您告诉我的内容,但无法编译。 link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多