C#如何通过网络从另一台PC访问配置文件

时间:2017-08-29 00:58:40

标签: c# networking server settings config

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

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; 

1 个答案:

答案 0 :(得分: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