简体   繁体   English

类库的C#配置

[英]c# configuration for class library

I have a class library which by default doesn't have an app.config. 我有一个类库,默认情况下没有app.config。 The calling app for this library is "explorer.exe" and I won't be able to use explorer.exe.config to add my settings. 该库的调用应用程序是“ explorer.exe”,我将无法使用explorer.exe.config添加设置。

Is there any way I can have my class library read an app.config? 有什么办法可以让我的类库读取app.config吗? It needs to be an app.config because I intend on encrypting it during deployment using aspnet_regiis (I'll rename it web.config, encrypt it and rename it back to app.config). 它必须是一个app.config,因为我打算在部署过程中使用aspnet_regiis对其进行加密(我将其重命名为web.config,对其进行加密,然后将其重命名为app.config)。

In C# the only config that matters really is the app.config of the output project. 在C#中,唯一真正重要的配置是输出项目的app.config In the case of a console app this will be the .exe config. 对于控制台应用程序,这将是.exe配置。 Which will appear in the bin as {your app name}.exe.config . 它将在bin显示为{your app name}.exe.config

You can read this file using the ConfigurationManager in the System.Configuration DLL. 您可以使用System.Configuration DLL中的ConfigurationManager读取此文件。 All the uses of this will point to the executing code's configuration file, even in a class library. 所有这些用法都将指向执行代码的配置文件,即使在类库中也是如此。 So any additional configuration needed in an imported class library will need to be added to this file. 因此,导入的类库中所需的任何其他配置都需要添加到此文件中。 This is the canonical way of dealing with config. 这是处理config的规范方法。

If you really want to have some other configuration file, you can use: 如果您确实想要其他配置文件,则可以使用:

ConfigurationManager.OpenMappedExeConfiguration(
            new ExeConfigurationFileMap
            {
                ExeConfigFilename = overrideConfigFileName
            }, 
            ConfigurationUserLevel.None)

Where overrideConfigFileName points to your other app.config file. 这里的overrideConfigFileName指向另一个app.config文件。 You can set the file in the class library as Content and ensure it is copied into the output directory at build time. 您可以在类库中将文件设置为Content并确保在构建时将其复制到输出目录中。 Then you will have to ensure that it is included in the final deploy package and all the paths match. 然后,您必须确保将其包含在最终的部署程序包中,并且所有路径都匹配。

In the end (as per @Stand__Sure and @tigerswithguitars I created a new project within my solution which will be a console App. It will be executed at deployment. Thanks to Stand__Sure for his link to https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-use-data-protection 最后(根据@Stand__Sure和@tigerswithguitars,我在解决方案中创建了一个新项目,该项目将是控制台应用程序。它将在部署时执行。感谢Stand__Sure链接至https://docs.microsoft.com/ zh-CN / dotnet / standard / security / how-to-use-data-protection

The console app does the following: 控制台应用程序执行以下操作:

private static void Run()
{
    try
    {
        // Get unencrypted data from Settings.dat
        string[] unencrypted = File.ReadAllLines("C:\\Program Files (x86)\\theAPPSettings\\Settings.dat");

        string unencryptedGuid = unencrypted[0]; //its only 1 setting that I'm interested in

        // Create a file.
        FileStream fStream = new FileStream("C:\\Program Files (x86)\\theAPPSettings\\ProtectedSettings.dat", FileMode.OpenOrCreate);

        byte[] toEncrypt = UnicodeEncoding.ASCII.GetBytes(unencryptedGuid);                

        byte[] entropy = UnicodeEncoding.ASCII.GetBytes("A Shared Phrase between the encryption and decryption");

        // Encrypt a copy of the data to the stream.
        int bytesWritten = Protection.EncryptDataToStream(toEncrypt, entropy, DataProtectionScope.CurrentUser, fStream);

        fStream.Close();

        File.Delete("C:\\Program Files (x86)\\theAPPSettings\\Settings.dat");

        //Console.ReadKey();
    }
    catch (Exception e)
    {
        Console.WriteLine("ERROR: " + e.Message);
    }
}

The calling app decrypts it as follows: 调用应用程序将其解密如下:

FileStream fStream = new FileStream("C:\\Program Files (x86)\\theAPPSettings\\ProtectedSettings.dat", FileMode.Open);

byte[] entropy = UnicodeEncoding.ASCII.GetBytes("A Shared Phrase between the encryption and decryption");

// Read from the stream and decrypt the data.
byte[] decryptData = Protection.DecryptDataFromStream(entropy, DataProtectionScope.CurrentUser, fStream, Length_of_Stream);

fStream.Close();

string temp = UnicodeEncoding.ASCII.GetString(decryptData);

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

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