我知道我可以使用以下代码行从其他位置加载app.config文件:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", ConfigFile);

其中ConfigFile是完整路径位置。我想做的是能够加载已为app.config加密的文件。理想情况下,我希望能够加载文件,解密它,并将其加载到字符串或内存流中,并将其传递给应用程序,就像它是app.config一样。我知道我可以从中加载所有值并手动访问它们,但我希望能够使用.NET的内置功能访问它们。有没有办法告诉应用程序使用除文件以外的配置文件?

另一种选择是打开文件,解密它,将其写入临时文件,然后使用上面的代码以这种方式引用它,但如果有一种更简单的方法,理想情况下,我想找到它,必须避免处理其他文件。

有帮助吗?

解决方案

虽然到目前为止我还没能得到答案,但我不得不想出一个解决办法。这可能不是最好的解决方案,但确实有效。基本上我们所做的是加密我们的app.config文件,并给它一个新名称。当应用程序启动时,它将获取已加密的文件,将其解密,并将其写入Windows临时文件。这确保了文件是一个没有人可能找到的唯一随机名称,我们不必管理文件,因为Windows会自动为我们删除它。这样每次重新启动我们都能够重新写出一个新文件并使用它。以下是感兴趣的任何人的基本代码片段。

第一种方法 LoadFileAppConfig()将加载文件。在这种情况下,由于它们是服务,我们需要加载执行路径,并将其传递给适当的方法。我们回到解密的app.config的路径,然后使用 SetData()方法将其设置为app.config路径。

/// <summary>
/// Loads the Local App.Config file, and sets it to be the local app.config file
/// </summary>
/// <param name="p_ConfigFilePath">The path of the config file to load, i.e. \Logs\</param>
public void LoadFileAppConfig(string p_ConfigFilePath)
{
    try
    {
        // The app.config path is the passed in path + Application Name + .config
        m_LocalAppConfigFile = ProcessLocalAppConfig(p_ConfigFilePath + this.ApplicationName + ".config");

        // This sets the service's app.config property
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", m_LocalAppConfigFile);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

在这个方法中,我们获取文件的路径,传递该文件以进行解密并作为字符串返回,然后将该文件写入我们的Windows临时文件。

public string ProcessLocalAppConfig(string p_ConfigFilePath)
{
    try
    {
        string fileName = Path.GetTempFileName();
        string unencryptedConfig = DecryptConfigData(p_ConfigFilePath);

        FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        StreamWriter streamWriter = new StreamWriter(fileStream);

        if (!string.IsNullOrEmpty(unencryptedConfig))
        {
            try
            {
                streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                streamWriter.WriteLine(unencryptedConfig);
            }

            catch (IOException ex)
            {
                Debug.Assert(false, ex.ToString());
            }
            finally
            {
                streamWriter.Close();
            }
            return fileName;
        }
        return null;
    }
    catch (Exception)
    {
        throw;
    }
}

这个最后的方法接受加密的app.config的路径,使用我们的解密工具解密文件(确保我们可以解密它,并且它是正确的文件类型),然后将解密的内容作为字符串到上面的方法。

private string DecryptConfigData(string p_AppConfigFile)
{
    string decryptedData = null;
    TMS.Pearl.SystemFramework.CryptographyManager.CryptographyManager cryptManager = new TMS.Pearl.SystemFramework.CryptographyManager.CryptographyManager();
    try
    {
        //Attempt to load the file.
        if (File.Exists(p_AppConfigFile))
        {
            //Load the file's contents and decrypt them if they are encrypted.
            string rawData = File.ReadAllText(p_AppConfigFile);

            if (!string.IsNullOrEmpty(rawData))
            {
                if (!rawData.Contains("<?xml"))  //assuming that all unencrypted config files will start with an xml tag...
                {
                    decryptedData = cryptManager.Decrypt(rawData);
                }
                else
                {
                    decryptedData = rawData;
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }

    return decryptedData;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top