相关疑难解决方法(0)

C#配置文件

好的,所以提前一段时间我发布了如何阅读其他程序的其他配置文件(这里是链接上一篇文章.我设法做到了.但是现在还有另外一个问题.场景是这样的,我有两个程序.程序A从配置文件读取其配置,程序B仅用于修改A读取的配置文件的内容.配置文件的名称是email.config.它位于程序AB所在的目录中.

问题是我使用打开文件对话框获取附件文件的路径.如果路径指向同一目录中的文件,则程序运行完美!但如果它指向目录外的文件,则会抛出System.NullReferenceException类型的异常.

这是代码

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 = …
Run Code Online (Sandbox Code Playgroud)

.net c# config file

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

config ×1

file ×1