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

问题描述:

我想从访问配置文件来自另一台计算机的例子。C#如何通过网络从另一台PC访问配置文件

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

你必须使用UNC网络路径读它。 –

+1

@JeremyThompson你可以给我们一些UNC网络的例子。 –

+1

@JeremyThompson我得到了我的问题的答案,我用'string server = doc.DocumentElement.SelectSingleNode(“Hostname”)。InnerText;'来获得服务器。 –

//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://*.com/a/15726277/495455
编号:https://*.com/a/9763947/495455


如果您遇到问题与ConfigurationManager.OpenExeConfiguration尝试System.Xml.Linq的,而不是

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


另一种方式来加载一个特定的exe的配置文件:

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

编号:https://*.com/a/12587078/495455

+1

我想要的是访问此连接'string location = @“\\ SERVER \ Shared \ Ramgy \ Settings.config”; '并提取服务器,端口,用户名等谢谢 –

+1

'string s = config.AppSettings.Settings [“Something”]。Value.ToString();'对象引用未设置为对象的实例。 –