<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ライブラリ | 株式会社ファーストペンギン</title>
	<atom:link href="https://www.firstpenguin.co.jp/tag/%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.firstpenguin.co.jp</link>
	<description></description>
	<lastBuildDate>Wed, 10 Jun 2020 09:16:05 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.firstpenguin.co.jp/wp-content/uploads/2019/09/cropped-icon-32x32.png</url>
	<title>ライブラリ | 株式会社ファーストペンギン</title>
	<link>https://www.firstpenguin.co.jp</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<atom:link rel="hub" href="https://websubhub.com/hub"/>
<atom:link rel="self" href="https://www.firstpenguin.co.jp/tag/%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA/feed/"/>
	<item>
		<title>C# 自作DLLからapp.configの読み書き</title>
		<link>https://www.firstpenguin.co.jp/c-appconfg/</link>
					<comments>https://www.firstpenguin.co.jp/c-appconfg/#respond</comments>
		
		<dc:creator><![CDATA[takashima.kentaro]]></dc:creator>
		<pubDate>Wed, 10 Jun 2020 09:16:49 +0000</pubDate>
				<category><![CDATA[技術情報]]></category>
		<category><![CDATA[開発]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[App.config]]></category>
		<category><![CDATA[DLL]]></category>
		<category><![CDATA[ライブラリ]]></category>
		<guid isPermaLink="false">https://www.firstpenguin.co.jp/?p=299</guid>

					<description><![CDATA[自作DLLからアプリケーション設定を読み書きする方法についてご紹介。 目次 アプリケーションからの読み書き方法自作DLLからの読み書き方法 アプリケーションからの読み書き方法 アプリケーション自身のアプリケーション設定を [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>自作DLLからアプリケーション設定を読み書きする方法についてご紹介。</p>

  <div id="toc" class="toc tnt-number-detail toc-center tnt-number_detail border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">アプリケーションからの読み書き方法</a></li><li><a href="#toc2" tabindex="0">自作DLLからの読み書き方法</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">アプリケーションからの読み書き方法</span></h2>
<p>アプリケーション自身のアプリケーション設定を読み書きする場合は以下の方法で簡単に操作できます。<br />
<img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-300" src="https://www.firstpenguin.co.jp/wp-content/uploads/c_appconfig_1.png" alt="" width="830" height="456" srcset="https://www.firstpenguin.co.jp/wp-content/uploads/c_appconfig_1.png 830w, https://www.firstpenguin.co.jp/wp-content/uploads/c_appconfig_1-300x165.png 300w, https://www.firstpenguin.co.jp/wp-content/uploads/c_appconfig_1-768x422.png 768w, https://www.firstpenguin.co.jp/wp-content/uploads/c_appconfig_1-406x223.png 406w" sizes="(max-width: 830px) 100vw, 830px" /></p>
<p>設定内容（Sample）を取得する場合は以下のように記述することで取得できます。</p>
<div></div>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-csharp" data-lang="C#"><code>var value = Properties.Settings.Default.Sample; 
Console.WriteLine(value); //出力結果：サンプル</code></pre>
</div>
<p>変更したい場合は以下の通りです。</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-csharp" data-lang="C#"><code>Properties.Settings.Default.Sample = "サンプル2";
var value = Properties.Settings.Default.Sample;
Console.WriteLine(value); //出力結果：サンプル2</code></pre>
</div>
<div></div>
<p>変更した値が反映されない場合は、以下の要領で再読み込みを行うと設定した内容が取得できます。</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-csharp" data-lang="C#"><code>//アプリケーション設定を再読み込み
Properties.Settings.Default.Reload();</code></pre>
</div>
<div></div>
<h2><span id="toc2">自作DLLからの読み書き方法</span></h2>
<p>別に作成したライブラリ(DLL)で呼び出し元のアプリケーション設定を読み書きする場合、Propertiesから取得しようとしても「Sample」とうプロパティ自体がありません。<br />
ライブラリから呼び出し元アプリケーション設定を取得する場合は参照設定より「System.Configuration」を追加し、以下のように記述します。</p>
<div>
<div></div>
</div>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-csharp" data-lang="C#"><code>var appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var group = appConfig.SectionGroups["userSettings"];
var section = (ClientSettingsSection)group.Sections[0];
var setting = section.Settings.Get("Sample");
//値を取得
var value = setting.Value.ValueXml.InnerText;
Console.WriteLine(value);//出力結果：サンプル</code></pre>
</div>
<p>また、値を書き込む場合は以下のように記述します。</p>
<p>&nbsp;</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-csharp" data-lang="C#"><code>//既存値を削除する
section.Settings.Remove(setting);
//値を変更
setting.Value.ValueXml.InnerText = "サンプル2";
//アプリケーション設定へ追加
section.Settings.Add(setting);

//呼び出し元のアプリケーションで出力してみる
//アプリケーション設定を再読み込み
Properties.Settings.Default.Reload();
Console.WriteLine(Properties.Settings.Default.Sample); //出力結果：サンプル2</code></pre>
</div>
<div></div>
<p>以上で自作DLLから呼び出し元アプリケーションの設定を読み書きする方法をご紹介しました。<br />
少し大変になりますが、必要になった場合はご参照ください。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.firstpenguin.co.jp/c-appconfg/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
