【问题标题】:C# Read ini value from urlC#从url读取ini值
【发布时间】:2015-07-07 12:14:34
【问题描述】:

我有一个网站,例如 "http://example.com",这里是 settings.ini 文件 "http://example.com/settings.ini" ,在文件中写了这个文本:

[Client]
Enabled=1

我不想从 C# 中给出那个值,有可能吗,怎么做?

例如我正在使用这个代码:

var MyIni = new IniFile(@"C:\settings.ini");
var DefaultVolume = MyIni.Read("Enabled");
MessageBox.Show(DefaultVolume);

效果很好,我正在尝试制作这样的东西:

var MyIni = new IniFile(@"http://example.com/settings.ini");

但它不起作用,谢谢。

编辑:

我收到此错误:

An unhandled exception of type "System.ArgumentException" in mscorlib.dll
For more information: URI formats are not supported.

更新:

此代码从 .ini 中获取所有价值,现在我需要集成到我的旧代码中

WebClient client = new WebClient();
Stream stream = client.OpenRead("http://example.com/settings.ini");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();

【问题讨论】:

  • 我认为它的文件访问权限相关问题

标签: c# web ini


【解决方案1】:

使用 HttpWebRequest (http://www.csharp-station.com/HowTo/HttpWebFetch.aspx) 读取 ini 文件内容

【讨论】:

    【解决方案2】:

    您可以使用我的库从 INI 文件中检索您的设置:

    https://github.com/MarioZ/MadMilkman.Ini

    例如像下面这样:

    WebClient client = new WebClient();
    IniFile myIni = new IniFile();
    
    myIni.Load(client.OpenRead("http://example.com/settings.ini"));
    
    string defaultVolume = myIni.Sections["Client"].Keys["Enabled"].Value;
    MessageBox.Show(defaultVolume);
    

    也只是作为一个 FYI,您可以将该值作为整数检索,如下所示:

    int volume;
    myIni.Sections["Client"].Keys["Enabled"].TryParseValue(out volume);
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2014-02-20
      • 2022-01-21
      • 1970-01-01
      • 2018-06-29
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多