C#配置文件 [英] C# Configuration Files
问题描述
好了,一会我提前发布了如何读取其他程序的其他配置文件(这里是链接的 previous帖子,我成功地做到这一点。但是现在有一个问题,这个场景是这样的,我有两个节目。节目的 A 从读取其配置一个配置文件和程序的 B 只能用于修改配置文件其内容的 A 的读取。config文件的名称是 email.config 。EM>这是在同一个目录中,程序的 A &安培; B 位于
Okay, so a while ahead I posted how to read other config files of other programs (here is the link Previous Post. I managed to do it. But now there is another problem. The scenario is like this, I have two programs. Program A reads its configuration from a config file and program B is only used to modify the contents of the config file which A reads. The name of the config file is email.config. It is in the same directory in which program A & B resides.
现在的问题是,我得到了使用打开文件对话框中的附件文件的路径。如果路径指向同一个目录中的文件,程序运行完美!但是,如果其指向的目录以外的文件时,它抛出一个异常类型的 System.NullReferenceException 的。
The problem is that I get path of a file for attachment using open file dialog. If the path points to a file in the same directory, the program works perfect! But if it points to a file outside the directory it throws an exception of type System.NullReferenceException.
下面是code
private void saveBtn_Click(object sender, EventArgs e)
{
try
{
// save everything and close
string attachment = attachTxtBox.Text;
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
// it throws exception here when
// the path points to a file outside the exes directory
Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
externalConfig.AppSettings.Settings["ServerAddress"].Value = serverAddr;
externalConfig.AppSettings.Settings["Port"].Value = port;
externalConfig.AppSettings.Settings["SSL"].Value = ssl.ToString();
externalConfig.AppSettings.Settings["Sender"].Value = senderAddr;
externalConfig.AppSettings.Settings["SenderPassword"].Value = password;
externalConfig.AppSettings.Settings["Subject"].Value = subject;
externalConfig.AppSettings.Settings["AttachmentPath"].Value = attachment;
externalConfig.AppSettings.Settings["Body"].Value = messageBody;
// Save values in config
externalConfig.Save(ConfigurationSaveMode.Full);
Application.Exit();
}
catch (System.Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
Application.Exit();
}
}
的内容的 email.config 的是:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="">
<clear />
<add key="ServerAddress" value="" />
<add key="Port" value="" />
<add key="Sender" value="" />
<add key="SenderPassword" value="" />
<add key="Subject" value="" />
<add key="AttachmentPath" value="" />
<add key="Body" value="" />
</appSettings>
</configuration>
我在做什么错在这里?
What am I doing wrong here?
编辑:的 configFileName 的值是email.config的
推荐答案
嗯,我理解了它自己调试了近5小时后,妈的!
Well, I figured it out myself after debugging for almost 5 hours, Damn!
但问题是,当我用打开文件对话框,以获得文件的路径,它改变了当前目录是在对话框中选择一个,所以程序找不到配置文件。我所做的只是设置打开文件对话框,以真实的RestoreDirectory财产的噗的,它的工作
The problem was when I used OpenFileDialog to get the file path, it changed the current directory to the one which is selected in the dialog, so the program couldn't find the config file. All I did was to set the RestoreDirectory property of OpenFileDialog to true and poof it worked
谢谢大家,ChrisF,约恩·坎贝尔和pablito。
Thanks everyone, ChrisF, Eoin Campbell and pablito.
这篇关于C#配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!