C# dll method call fails to write and read to/from the registry when called from ASP.NET web page

此生再无相见时 提交于 2021-02-11 12:32:00

问题


I have a class within a dll that has the following methods for writing to the registry:

public Boolean WriteDBConnectionInfoToRegistry(string txtDSN, string txtServer, string txtDatabase, string txtUser, string txtPassword, bool trusted)
        {
            SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
            rijn.GenerateKey();
            rijn.GenerateIV();
            Boolean b1 = RegistryWrite("Software\\MyApp", "IV", Reverse(Convert.ToBase64String(rijn.IV)));
            Boolean b2 = RegistryWrite("Software\\MyApp", "Key", Reverse(Convert.ToBase64String(rijn.Key)));
            Boolean b3 = RegistryWrite("Software\\MyApp", "DSN", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtDSN))));
            Boolean b4 = RegistryWrite("Software\\MyApp", "Server", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtServer))));
            Boolean b5 = RegistryWrite("Software\\MyApp", "Database", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtDatabase))));
            Boolean b6 = RegistryWrite("Software\\MyApp", "User", EncryptString(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtUser))),rijn.IV,rijn.Key));
            Boolean b7 = RegistryWrite("Software\\MyApp", "Password", EncryptString(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtPassword))), rijn.IV, rijn.Key));
            Boolean b8 = RegistryWrite("Software\\MyApp", "Trusted", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(trusted ? "1" : "0"))));

            if (b1 && b2 && b3 && b4 & b5 & b6 & b7 & b8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


  private bool RegistryWrite(string subKey, string KeyName, object Value)
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey sk1 = rk.CreateSubKey(subKey);
                sk1.SetValue(KeyName.ToUpper(), Value);
                return true;
            }
            catch (Exception e)
            {
            }
            return false;
        }

The methods are contained within a public class, that is part of a dll that I have installed in a ASP.NET web page. The methods successfully write to the registry when called from a Windows application to test, however when the method is called from the code behind in the web site, it fails to write to create the folder and keys, despite returning true. How can I fix this? The framework for the .dll is .Net 2.0 and the web application is in 4.0. Thank you for the help.

来源:https://stackoverflow.com/questions/13631235/c-sharp-dll-method-call-fails-to-write-and-read-to-from-the-registry-when-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!