ConfigurationManager中和的AppSettings在通用(UWP)应用 [英] ConfigurationManager and AppSettings in universal (UWP) app

查看:189
本文介绍了ConfigurationManager中和的AppSettings在通用(UWP)应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储在配置文件中的API密钥不检查其源代码控制,并阅读我UWP应用程序中的数据。

I would like to store an API key in a configuration file without checking it into source control, and read the data in my UWP app.

一个常见的​​解决方案是在config文件的密钥存储区(如的app.config 的web.config ),并访问它像这样

A common solution is to store the key in .config file (such as app.config or web.config) and access it like so:

var apiKey = ConfigurationManager.AppSettings.Get("apiKey");



我工作的一个通用的Windows(UWP)应用程序,并不能访问System.Configuration命名空间包含 ConfigurationManager中

我怎样才能访问UWP应用的AppSettings?
或者,什么是访问的UWP应用配置数据的最佳方式是什么?

How can I access AppSettings in UWP app? Alternatively, what's the best way to access configuration data in an UWP app?

推荐答案

在我的具体使用情况下,我使用不是由源控制跟踪外部文件需要的。有访问从资源或配置文件数据两种方法

In my specific use case I needed to use an external file that is not tracked by source control. There are two ways to access data from resource or configuration files.

一是打开和分析的配置文件。给定一个文件 sample.txt的生成操作 内容复制到输出目录没关系),我们可以用

One is to open and parse a configuration file. Given a file sample.txt with Build Action Content (Copy to Output Directory doesn't matter), we can read it with

var uri = new System.Uri("ms-appx:///sample.txt");
var sampleFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

var packageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var sampleFile = await packageFolder.GetFileAsync("sample.txt");



其次

followed by

var contents = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);



此外,我们可以使用资源即可。一个新的资源项目添加到项目中,名为 resourcesFile.resw 。要访问数据,使用:

Alternatively, we can use Resources. Add a new Resource item to the project, called resourcesFile.resw. To access data, use:

var resources = new Windows.ApplicationModel.Resources.ResourceLoader("resourcesFile");
var token = resources.GetString("secret");



我在一篇博客文章中写道更详细的答案的自定义资源的UWP

这篇关于ConfigurationManager中和的AppSettings在通用(UWP)应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆