简体   繁体   English

有人可以向我解释设置以及如何在C#中使用它们吗?

[英]Can someone explain Settings to me and how to use them in C#?

Basically something I'm working on requires me to use Settings. 基本上,我正在做的事情要求我使用“设置”。 I've never used Settings when it comes to C# and VS and the guides I've tried reading on the internet aren't really clearing it up for me. 在C#和VS方面,我从未使用过设置,而我尝试在互联网上阅读的指南并没有真正为我清除这些设置。 So I've got a few questions and if anyone could answer them then that would be brilliant. 所以我有几个问题,如果有人可以回答,那就太好了。

  1. What is a setting and what's it's purpose? 什么是设置,目的是什么?
  2. I've declared some settings, now how do I use them in my code? 我已经声明了一些设置,现在如何在代码中使用它们?
  3. Can I declare a setting as a variable or are they already variables that can be used? 我可以将设置声明为变量,还是它们已经可以使用?

An example is that I've got these codes in my Settings "BL,CC,CW,DX,DL,DU,F2,HA,JL,LD,PR,PS,S,SC,SF,UD,WL,ZC,ZD", they're called Codes 一个例子是我在“ BL,CC,CW,DX,DL,DU,F2,HA,JL,LD,PR,PS,S,SC,SF,UD,WL,ZC, ZD”,它们称为代码

  1. how can I use these Codes in the context of my code that when something is equal to one of these codes, do something else? 我如何在我的代码上下文中使用这些代码,即当某些内容等于这些代码之一时,还要执行其他操作?

Using Settings in C# 在C#中使用设置

What is a setting and what's it's purpose? 什么是设置,目的是什么?

The .NET Framework [..] allows you to create and access values that are persisted between application execution sessions . .NET Framework [..]允许您创建和访问在应用程序执行会话之间保留的值 These values are called settings. 这些值称为设置。 Settings can represent user preferences, or valuable information the application needs to use . 设置可以代表用户首选项或应用程序需要使用的有价值的信息 For example, you might create a series of settings that store user preferences for the color scheme of an application. 例如,您可以创建一系列设置来存储用户对应用程序配色方案的首选项。

How do I use them in my code? 如何在代码中使用它们? Are they already variables that can be used? 它们已经是可以使用的变量了吗?

You can read both application-scope and user-scope settings at run time with the Properties namespace. 您可以在运行时使用Properties命名空间读取应用程序范围和用户范围的设置。 The Properties namespace exposes all of the default settings for the project by using the Properties.Settings.Default object. Properties命名空间使用Properties.Settings.Default对象公开了项目的所有默认设置。 When writing code that uses settings, all settings appear in IntelliSense and are strongly typed . 编写使用设置的代码时, 所有设置都将显示在IntelliSense中,并具有强类型性 Thus, if you have a setting that is of type System.Drawing.Color, for example, you can use it without having to cast it first, as shown in the following example: 因此,例如,如果您有一个类型为System.Drawing.Color的设置,则可以使用它而不必先对其进行铸造,如以下示例所示:

 this.BackColor = Properties.Settings.Default.myColor; 

As for your specific question, you might want to look into something like string.Contains() . 至于您的特定问题,您可能需要研究一下string.Contains()类的东西。

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

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