初期設定の読み書き
Posted feedbacks - C#
お題のJavaを.NETに移植した感じ。 System.Configuration.dllを参照してビルドすること。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | using System;
using System.Configuration;
class HelloPreference
{
const string MESSAGE_KEY = "message";
const string DEFAULT_MESSAGE = "Hello, Preference.";
public string Message { get; set; }
public Configuration Configuration { get; set; }
public HelloPreference()
{
LoadPreference();
}
public void LoadPreference()
{
Configuration conf = ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None);
this.Configuration = conf;
KeyValueConfigurationElement element
= conf.AppSettings.Settings[MESSAGE_KEY];
this.Message = element != null
? element.Value
: DEFAULT_MESSAGE;
}
public void StorePreference()
{
Configuration conf = this.Configuration;
KeyValueConfigurationCollection settings
= conf.AppSettings.Settings;
KeyValueConfigurationElement elem
= settings[MESSAGE_KEY];
if (elem != null)
{
elem.Value = this.Message;
}
else
{
settings.Add(MESSAGE_KEY, this.Message);
}
conf.Save();
}
public void ShowMessage()
{
Console.WriteLine(this.Message);
}
static void Main(string[] args)
{
try
{
HelloPreference hello = new HelloPreference();
if (args.Length > 0)
{
hello.Message = args[0];
}
hello.ShowMessage();
hello.StorePreference();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
|


lunlumo #9190() [ Java ] Rating-2/4=-0.50
初期設定を読み書きするプログラムを書いてください。
保存先や形式は問いませんが,OS,ライブラリ,言語等の環境で標準的なものがあれば,なるべくそちらを用いてください。
Rating-2/4=-0.50-0+
[ reply ]