2017-08-29 78 views
0

我想從訪問配置文件來自另一臺計算機的例子。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;'來獲得服務器。 –

回答

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

+1

我想要的是訪問此連接'string location = @「\\ SERVER \ Shared \ Ramgy \ Settings.config」; '並提取服務器,端口,用戶名等謝謝 –

+1

'string s = config.AppSettings.Settings [「Something」]。Value.ToString();'對象引用未設置爲對象的實例。 –

相關問題