【问题标题】:C# how to access config file from another PC over the networkC#如何通过网络从另一台PC访问配置文件
【发布时间】:2018-02-06 07:50:20
【问题描述】:

我想访问另一台计算机的配置文件示例。

string location = @"\\SERVER\Shared\Ramgy\Settings.config"

// get server from **SERVER** location
this.txthostname.Text = Properties.Settings.Default.server; 

// get port from the SERVER location
this.txtport.Text = Properties.Settings.Default.port; 

//get username from the SERVER location
this.txtusername.Text = Properties.Settings.Default.username;

// get password from the SERVER location
this.txtpassword.Text = Properties.Settings.Default.password;

// get database from the SERVER location
this.txtdatabase.Text = Properties.Settings.Default.database; 

【问题讨论】:

  • 您必须使用 UNC 网络路径读取它。
  • @JeremyThompson 你能给我们一些UNC网络的例子吗?
  • @JeremyThompson 我得到了我使用string server = doc.DocumentElement.SelectSingleNode("Hostname").InnerText; 获取服务器的问题的答案。
  • @JeremyThompson 我使用的是 XmlDocument 而不是 UNC
  • 您能否将我的答案标记为正确,我的 Linq XML 方法链接有效?是的。它会给你一些分数,让大家知道问题已经解决了

标签: c# networking server settings config


【解决方案1】:
//Reading a Dlls own config:
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll

//Read a colleagues config file (the settings.settings are stored in config):
location = "\\JoeBlogsPC\c$\AppPath\Search.dll";

var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections;
string s = config.AppSettings.Settings["Something"].Value.ToString();

参考:https://stackoverflow.com/a/15726277/495455
参考:https://stackoverflow.com/a/9763947/495455


如果您被 ConfigurationManager.OpenExeConfiguration 卡住,请尝试使用 System.Xml.Linq

https://stackoverflow.com/a/42939187/495455


另一种加载特定exe配置文件的方法:

ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 

参考:https://stackoverflow.com/a/12587078/495455

【讨论】:

  • 我只想访问此连接string location = @"\\SERVER\Shared\Ramgy\Settings.config"; 并提取服务器、端口、用户名等。谢谢
  • string s = config.AppSettings.Settings["Something"].Value.ToString(); 对象引用未设置为对象的实例。
猜你喜欢
  • 2016-12-15
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 2020-05-23
  • 2015-06-25
  • 2019-03-02
  • 2013-02-06
相关资源
最近更新 更多