C#如何通过网络从另一台PC访问配置文件
C# how to access config file from another PC over the network
我想访问来自另一台计算机示例的配置文件。
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;
//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();
参考:
参考:
如果您无法使用 ConfigurationManager.OpenExeConfiguration
,请尝试使用 System.Xml.Linq
加载特定exe配置文件的另一种方式:
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
参考:
我想访问来自另一台计算机示例的配置文件。
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;
//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();
参考:
参考:
如果您无法使用 ConfigurationManager.OpenExeConfiguration
,请尝试使用 System.Xml.Linq
加载特定exe配置文件的另一种方式:
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
参考: