如何使用.NET WPF Click-Once存储持久性用户连接字符串

时间:2017-05-22 08:54:13

标签: c# .net wpf clickonce

我们正在开发一个Click-Once WPF Windows应用程序,用于从数据库中检索其所有数据。我实现了一个表单,用户可以在其中输入相应的数据,并使用SqlConnectionStringBuilder构建连接字符串,并且工作正常。

然后我们使用

将连接字符串添加到ConfigurationManager
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

如果我们使用其他一些ConfigurationUserLevel,应用程序崩溃(我不知道为什么,但我相信实体框架模型试图加载连接字符串而找不到它,因为该文件没有存储在正确的用户级别?)。现在,我们将连接字符串存储在一个单独的文件中并将其加载到App.config中,这样我们就不必将其检入Version Control:

<connectionStrings configSource="connections.config"/>

我们不部署此文件,因为它包含我们自己的开发连接字符串。相反,我们创建一个用于部署的空文件,用于存储用户输入的连接字符串。这完全没问题。

我们的问题是,点击一次更新后,文件将“丢失”。存储在配置文件中加密的持久性每用户连接字符串的最佳方法是什么?或者我们应该完全切换到注册表?或者在%APPDATA%/Local/Apps/2.0/...../文件夹之外的某处创建我们自己的加密文件,并在初始化实体框架上下文时手动加载它?

1 个答案:

答案 0 :(得分:1)

您可以访问它可能会帮助您的MSDN网站

https://msdn.microsoft.com/en-us/library/dd997001.aspx 在下面的代码的帮助下,您需要根据您的要求修改此代码,希望这会有所帮助。

创建自定义ClickOnce应用程序安装程序

在ClickOnce应用程序中,添加对System.Deployment和System.Windows.Forms的引用。

向您的应用程序添加一个新类并指定任何名称。本演练使用名称MyInstaller。

将以下Imports或using语句添加到新类的顶部。

using System.Deployment.Application;  
using System.Windows.Forms; 

将以下方法添加到您的班级。

这些方法调用InPlaceHostingManager方法来下载部署清单,断言相应的权限,询问用户是否有安装权限,然后将应用程序下载并安装到ClickOnce缓存中。自定义安装程序可以指定ClickOnce应用程序是预先信任的,或者可以将信任决策推迟到AssertApplicationRequirements方法调用。此代码预先信任该应用程序 注意: - 预信任分配的权限不能超过自定义安装程序代码的权限。

InPlaceHostingManager iphm = null;

    public void InstallApplication(string deployManifestUriStr)
    {
        try
        {
            Uri deploymentUri = new Uri(deployManifestUriStr);
            iphm = new InPlaceHostingManager(deploymentUri, false);
        }
        catch (UriFormatException uriEx)
        {
            MessageBox.Show("Cannot install the application: " + 
                "The deployment manifest URL supplied is not a valid URL. " +
                "Error: " + uriEx.Message);
            return;
        }
        catch (PlatformNotSupportedException platformEx)
        {
            MessageBox.Show("Cannot install the application: " + 
                "This program requires Windows XP or higher. " +
                "Error: " + platformEx.Message);
            return;
        }
        catch (ArgumentException argumentEx)
        {
            MessageBox.Show("Cannot install the application: " + 
                "The deployment manifest URL supplied is not a valid URL. " +
                "Error: " + argumentEx.Message);
            return;
        }

        iphm.GetManifestCompleted += new EventHandler<GetManifestCompletedEventArgs>(iphm_GetManifestCompleted);
        iphm.GetManifestAsync();
    }

    void iphm_GetManifestCompleted(object sender, GetManifestCompletedEventArgs e)
    {
        // Check for an error.
        if (e.Error != null)
        {
            // Cancel download and install.
            MessageBox.Show("Could not download manifest. Error: " + e.Error.Message);
            return;
        }

        // bool isFullTrust = CheckForFullTrust(e.ApplicationManifest);

        // Verify this application can be installed.
        try
        {
            // the true parameter allows InPlaceHostingManager
            // to grant the permissions requested in the applicaiton manifest.
            iphm.AssertApplicationRequirements(true) ; 
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while verifying the application. " +
                "Error: " + ex.Message);
            return;
        }

        // Use the information from GetManifestCompleted() to confirm 
        // that the user wants to proceed.
        string appInfo = "Application Name: " + e.ProductName;
        appInfo += "\nVersion: " + e.Version;
        appInfo += "\nSupport/Help Requests: " + (e.SupportUri != null ?
            e.SupportUri.ToString() : "N/A");
        appInfo += "\n\nConfirmed that this application can run with its requested permissions.";
        // if (isFullTrust)
        // appInfo += "\n\nThis application requires full trust in order to run.";
        appInfo += "\n\nProceed with installation?";

        DialogResult dr = MessageBox.Show(appInfo, "Confirm Application Install",
            MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        if (dr != System.Windows.Forms.DialogResult.OK)
        {
            return;
        }

        // Download the deployment manifest. 
        iphm.DownloadProgressChanged += new EventHandler<DownloadProgressChangedEventArgs>(iphm_DownloadProgressChanged);
        iphm.DownloadApplicationCompleted += new EventHandler<DownloadApplicationCompletedEventArgs>(iphm_DownloadApplicationCompleted);

        try
        {
            // Usually this shouldn't throw an exception unless AssertApplicationRequirements() failed, 
            // or you did not call that method before calling this one.
            iphm.DownloadApplicationAsync();
        }
        catch (Exception downloadEx)
        {
            MessageBox.Show("Cannot initiate download of application. Error: " +
                downloadEx.Message);
            return;
        }
    }

    /*
    private bool CheckForFullTrust(XmlReader appManifest)
    {
        if (appManifest == null)
        {
            throw (new ArgumentNullException("appManifest cannot be null."));
        }

        XAttribute xaUnrestricted =
            XDocument.Load(appManifest)
                .Element("{urn:schemas-microsoft-com:asm.v1}assembly")
                .Element("{urn:schemas-microsoft-com:asm.v2}trustInfo")
                .Element("{urn:schemas-microsoft-com:asm.v2}security")
                .Element("{urn:schemas-microsoft-com:asm.v2}applicationRequestMinimum")
                .Element("{urn:schemas-microsoft-com:asm.v2}PermissionSet")
                .Attribute("Unrestricted"); // Attributes never have a namespace

        if (xaUnrestricted != null)
            if (xaUnrestricted.Value == "true")
                return true;

        return false;
    }
    */

    void iphm_DownloadApplicationCompleted(object sender, DownloadApplicationCompletedEventArgs e)
    {
        // Check for an error.
        if (e.Error != null)
        {
            // Cancel download and install.
            MessageBox.Show("Could not download and install application. Error: " + e.Error.Message);
            return;
        }

        // Inform the user that their application is ready for use. 
        MessageBox.Show("Application installed! You may now run it from the Start menu.");
    }

    void iphm_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        // you can show percentage of task completed using e.ProgressPercentage
    }

要尝试从代码安装,请调用InstallApplication方法。例如,如果您将类命名为MyInstaller,则可以通过以下方式调用InstallApplication。

MyInstaller installer = new MyInstaller();  
installer.InstallApplication(@"\\myServer\myShare\myApp.application");  
MessageBox.Show("Installer object created.");