简体   繁体   English

C#配置文件

[英]C# Configuration Files

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

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 . 但如果它指向目录外的文件,则会抛出System.NullReferenceException类型的异常。

Here is the 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();
    }
}

The content of email.config is: 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? 我在这做错了什么?

EDIT: The value of configFileName is "email.config" 编辑: configFileName的值是“email.config”

Well, I figured it out myself after debugging for almost 5 hours, Damn! 好吧,经过近5个小时的调试,我自己想出来了,该死的!

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. 问题是当我使用OpenFileDialog获取文件路径时,它将当前目录更改为在对话框中选择的目录,因此程序找不到配置文件。 All I did was to set the RestoreDirectory property of OpenFileDialog to true and poof it worked 我所做的就是将OpenFileDialog的RestoreDirectory属性设置为true,并将其设置为poof

Thanks everyone, ChrisF, Eoin Campbell and pablito. 谢谢大家,ChrisF,Eoin Campbell和pablito。

Are you accessing the file by its full path or just the file name? 您是通过其完整路径还是仅通过文件名访问该文件?

If the latter then this will work when the file is in the same folder as the executable, but not otherwise. 如果是后者,那么当文件与可执行文件位于同一文件夹中时,这将起作用,但不是。

UPDATE UPDATE

It looks as though things are more complicated than I first thought and this doesn't seem to be the issue here - see the comments. 看起来事情比我原先想象的要复杂得多,这似乎不是问题 - 请参阅评论。 The line that's raising the exception is: 引发异常的那一行是:

externalConfig.AppSettings.Settings["ServerAddress"].Value = serverAddr;

So that means that there's a null reference somewhere along the chain. 这意味着在链的某处有一个空引用。 If you can identify which it is then that should give you a pointer to the problem. 如果你能识别它是什么,那么应该给你一个指向问题的指针。

externalConfig.AppSettings.Settings["SSL"].Value = ssl.ToString();

Config file does not contain "SSL" 配置文件不包含“SSL”

just my 2c for those that are trying this code. 对于那些尝试此代码的人来说,只是我的2c。

What code are you using to get the FileName and Path back from the OpenFileDialog. 您使用什么代码从OpenFileDialog获取FileName和Path。

Is it a fully qualified path to the file ? 它是文件的完全限定路径吗?

eg 例如

openFileDialog1.FileName; //Contains "C:\\Path\\To\\The\\File.txt"

By the sounds of it, whats being saved is only a filename, so your application is only looking in the current path. 通过它的声音,保存的只是一个文件名,因此您的应用程序只查看当前路径。

我有同样的问题,我不知道这是否可以帮助你,但当我更改配置文件的名称,这是在你的情况下,在另一个文件夹中,.config并且它不再崩溃,在我的我可以更改名称,所以我没有继续调查如何使其与其他名称一起工作,但我当然想知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM