ConfigurationManager.AppSettings Property

本文介绍了如何在app.config文件中添加配置项,并通过System.Configuration.dll引用进行访问。包括配置读取、添加新设置、更新现有设置等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在app.config文件中添加如下配置

 <appSettings>
    <add  key="Server" value="127.0.0.1"/>
    <add  key="Port" value="2020"/>
  </appSettings>

 

 

访问方式(需要添加System.Configuration.dll的引用)

  string server = ConfigurationManager.AppSettings["Server"];
  int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);

 

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

Gets the AppSettingsSection data for the current application's default configuration.

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public static NameValueCollection AppSettings { get; }
Property Value

Type: System.Collections.Specialized.NameValueCollection

Returns a NameValueCollection object that contains the contents of the AppSettingsSection object for the current application's default configuration.

Exceptions

ConfigurationErrorsException

Could not retrieve a NameValueCollection object with the application settings data.

Remarks

AppSettingsSection object contains the contents of the configuration file's appSettings section.

The first example shows a simple console application that reads application settings, adds a new setting, and updates an existing setting.

using System;
using System.Configuration;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadAllSettings();
            ReadSetting("Setting1");
            ReadSetting("NotValid");
            AddUpdateAppSettings("NewSetting", "May 7, 2014");
            AddUpdateAppSettings("Setting1", "May 8, 2014");
            ReadAllSettings();
        }

        static void ReadAllSettings()
        {
            try
            {
                var appSettings = ConfigurationManager.AppSettings;

                if (appSettings.Count == 0)
                {
                    Console.WriteLine("AppSettings is empty.");
                }
                else
                {
                    foreach (var key in appSettings.AllKeys)
                    {
                        Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
                    }
                }
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error reading app settings");
            }
        }

        static void ReadSetting(string key)
        {
            try
            {
                var appSettings = ConfigurationManager.AppSettings;
                string result = appSettings[key] ?? "Not Found";
                Console.WriteLine(result);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error reading app settings");
            }
        }

        static void AddUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error writing app settings");
            }
        }
    }
}

 

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="Setting1" value="May 5, 2014"/>
    <add key="Setting2" value="May 6, 2014"/>
  </appSettings>
</configuration>

 

转载于:https://www.cnblogs.com/chucklu/p/5280043.html

Dim sUrl As String '報表模組 Dim RPTKIND As String = "WIP" Dim Base_ID As String Dim Lot_ID As String Dim RECORD_ID As String Dim part_id As String Dim ver As String Dim Sql_m, sql, sql_korea, str As String Dim I As Integer Dim is_al_name As New ArrayList Dim is_al As New ArrayList Dim is_dt As New ArrayList Dim is_sql As New ArrayList Dim RPTSQL1 As String Dim RPTNAME1 As String Dim rpt_name As String Dim filename, filename2, filename3, Path As String Dim FLAG As Integer = 0 Dim log_sql As String Dim tag As String Dim sourcePath As String Dim targetPath As String Dim targetPath2 As String Dim J As Integer Dim fso As Object Dim ExcelApp As Object Dim Workbook As Excel.Workbook If MsgBox("請確認是否要列印所選擇的所有工單?", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Cancel Then Exit Sub End If tag = CStr(DateDiff(DateInterval.Second, New DateTime(1970, 1, 1), Now())) For I = 0 To DG_MAIN.Rows.Count - 1 If DG_MAIN.Rows(I).Cells("cbox").Value = True Then Base_ID = DG_MAIN.Rows(I).Cells("BASE_ID").EditedFormattedValue.ToString Lot_ID = DG_MAIN.Rows(I).Cells("THIS_LOT_ID").EditedFormattedValue.ToString part_id = DG_MAIN.Rows(I).Cells("new_part_id").EditedFormattedValue.ToString ver = DG_MAIN.Rows(I).Cells("this_ver").EditedFormattedValue.ToString RECORD_ID = DG_MAIN.Rows(I).Cells("RECORD_ID").EditedFormattedValue.ToString is_al_name.Clear() is_al.Clear() ' 原始檔案路徑與複製後的檔案路徑 filename = part_id + ver + "_工單" sourcePath = System.Configuration.ConfigurationManager.AppSettings("Path_save").ToString() & "\Public\System\Wpaper_Data\Before\" & filename & ".xlsx" targetPath = System.Configuration.ConfigurationManager.AppSettings("Path_save").ToString() & "\Public\System\Wpaper_Data\Before\" & filename & "_" & Base_ID & Lot_ID & ".xlsx" targetPath2 = System.Configuration.ConfigurationManager.AppSettings("Path_save").ToString() & "\Public\System\Wpaper_Data\Before\" & filename & "_" & Base_ID & Lot_ID & ".PDF" If File.Exists(sourcePath) Then Else MsgBox("指定路徑下暫未找到製作規範流程文檔,請確認!") Exit Sub End If ' 建立 FileSystemObject 來複製檔案 fso = CreateObject("Scripting.FileSystemObject") fso.CopyFile(sourcePath, targetPath, True) ' True 表示覆蓋已存在的檔案 ' 建立 Excel 應用程式物件 ExcelApp = CreateObject("Excel.Application") ExcelApp.Visible = True Workbook = ExcelApp.Workbooks.Open(targetPath) ' 這裡可以加入其他操作,例如讀取儲存格資料 ' 寫入資料到第一個工作表的 A1 儲存格 ' Workbook.Sheets(1).Cells(108, 18).Value = "這是寫入的資料" ' 寫入條碼文字(Code 39 需加 * 符號) Workbook.Sheets(1).Cells(109, 1).Value = "*" & Workbook.Sheets(1).Cells(108, 1).Value & "*" Workbook.Sheets(1).Cells(109, 7).Value = "*" & Base_ID & "*" Workbook.Sheets(1).Cells(109, 14).Value = "*" & Lot_ID & "*" Workbook.Sheets(1).Cells(109, 18).Value = "*" & Workbook.Sheets(1).Cells(108, 18).Value & "*" ' 設定字型為條碼字型(需已安裝) Workbook.Sheets(1).Cells(109, 1).Font.Name = "3 of 9 Barcode" Workbook.Sheets(1).Cells(109, 7).Font.Name = "3 of 9 Barcode" Workbook.Sheets(1).Cells(109, 14).Font.Name = "3 of 9 Barcode" Workbook.Sheets(1).Cells(109, 18).Font.Name = "3 of 9 Barcode" J = 110 For J = 120 To 150 If InStr(Workbook.Sheets(1).Cells(J, 5).Value, "Program:.") > 0 Then Workbook.Sheets(1).Cells(J, 20).Value = "*" & Mid(Workbook.Sheets(1).Cells(J, 5).Value, InStr(Workbook.Sheets(1).Cells(J, 5).Value, "Program:.") + 7, 10) & "*" Workbook.Sheets(1).Cells(J, 20).Font.Name = "3 of 9 Barcode" End If Next '轉成PDF 'Workbook.ExportAsFixedFormat(Type:=0, filename:=targetPath2, Quality:=0, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False) ' 儲存並關閉 Workbook.Save() ' 不關閉 Excel,讓使用者操作 Workbook = Nothing ExcelApp = Nothing fso = Nothing End If Next End Sub1. 加一個預覽工單的功能 2.配合勾選將工單Excel 產出對應的Excel 3.在各層別的背面工單加入 料號+生產工單+4碼工令+層別 的條碼 4.每一面次的背面工單都是由108行*面次~108行*面次+106 行 4.在背面工單判斷NOTE 欄位(E欄位),若有Program:.程式代號 ,關鍵字Program:.則在該欄位加入 條碼=*料號+程式代號 * 5.存成PDF後產出,利用網頁打開或是直接打開Excel(再討論)這樣就可以進行預覽列印
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值