8

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

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

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

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

4

1 回答 1

3

虽然到目前为止我一直无法得到答案,但我不得不想出一个解决方法。这可能不是最好的解决方案,但它确实有效。基本上我们所做的是加密我们的 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;
}
于 2009-02-05T22:28:32.397 回答