可以从字符串或内存流加载App.Config吗?

Noa*_*oah 8 .net c# app-config memorystream

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

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", ConfigFile);
Run Code Online (Sandbox Code Playgroud)

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

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

Noa*_*oah 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在此方法中,我们获取文件的路径,将该文件传递给解密并作为字符串返回,然后将该文件写入我们的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后一个方法接受加密的 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 任何具有更多 PC 知识的人都知道,可以使用 filemon 查看任何应用程序的所有 IO 写入。 (2认同)
  • 您应该知道很容易找到您的应用程序配置。 (2认同)