BusynessNotification

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 3e2923ec8c64b5d0b40a3197a7d4eaebf9602144
parent 39b96d557c021614f22cf13c6c79ad716f19ecad
Author: Minerva_juppiter <ryouturn@gmail.com>
Date:   Fri, 22 Mar 2024 11:13:30 +0900

設定ファイルをWPFのプロパティ型からjsonに変更

Diffstat:
MApp.xaml.cs | 1-
MBusynessNotification.csproj | 26+++++---------------------
MMainWindow.xaml.cs | 93++++++++++++++++++++++++++++++++++++++++++-------------------------------------
MObserveUseage.cs | 24++++++++++++++----------
DProperties/Settings.Designer.cs | 134-------------------------------------------------------------------------------
DProperties/Settings.settings | 34----------------------------------
ASettingJson.cs | 21+++++++++++++++++++++
ASettingJson.json | 12++++++++++++
8 files changed, 102 insertions(+), 243 deletions(-)

diff --git a/App.xaml.cs b/App.xaml.cs @@ -46,5 +46,4 @@ namespace BusynessNotification } } } - } diff --git a/BusynessNotification.csproj b/BusynessNotification.csproj @@ -28,6 +28,8 @@ </ItemGroup> <ItemGroup> + <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> + <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> <PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" /> <PackageReference Include="ModernWpfUI" Version="0.9.6" /> </ItemGroup> @@ -37,29 +39,11 @@ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Resource> </ItemGroup> - <ItemGroup> - <Compile Update="Properties\Settings.Designer.cs"> - <DesignTimeSharedInput>True</DesignTimeSharedInput> - <AutoGen>True</AutoGen> - <DependentUpon>Settings.settings</DependentUpon> - </Compile> - <Compile Update="Settings1.Designer.cs"> - <DesignTimeSharedInput>True</DesignTimeSharedInput> - <AutoGen>True</AutoGen> - <DependentUpon>Settings1.settings</DependentUpon> - </Compile> + <None Update="SettingJson.json"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> </ItemGroup> - <ItemGroup> - <None Update="Properties\Settings.settings"> - <Generator>SettingsSingleFileGenerator</Generator> - <LastGenOutput>Settings.Designer.cs</LastGenOutput> - </None> - <None Update="Settings1.settings"> - <Generator>SettingsSingleFileGenerator</Generator> - <LastGenOutput>Settings1.Designer.cs</LastGenOutput> - </None> - </ItemGroup> </Project> diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs @@ -10,6 +10,9 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Security.AccessControl; +using System.Text.Json; +using System.Diagnostics; +using Microsoft.Extensions.Configuration; namespace BusynessNotification { @@ -26,29 +29,33 @@ namespace BusynessNotification double SliderMemory = 50; double SliderDisk = 50; - int SecCPU = 5; - int SecMemory = 5; - int SecDisk = 5; + double SecCPU = 5; + double SecMemory = 5; + double SecDisk = 5; public MainWindow() { InitializeComponent(); ///設定値の読み取り + var configuration = new ConfigurationBuilder() + .AddJsonFile("SettingJson.json") + .Build(); try { - CheckCPU = Properties.Settings.Default.CheckCPU; - CheckMemory = Properties.Settings.Default.CheckMemory; - CheckDisk = Properties.Settings.Default.CheckDisk; - - SliderCPU = Properties.Settings.Default.SliderCPU; - SliderMemory = Properties.Settings.Default.SliderMemory; - SliderDisk = Properties.Settings.Default.SliderDisk; - - SecCPU = Properties.Settings.Default.SecCPU; - SecMemory = Properties.Settings.Default.SecMemory; - SecDisk = Properties.Settings.Default.SecDisk; - }catch (Exception) + CheckCPU = Convert.ToBoolean(configuration["CheckCPU"]); + CheckMemory = Convert.ToBoolean(configuration["CheckMemory"]); + CheckDisk = Convert.ToBoolean(configuration["CheckDisk"]); + + SliderCPU = Convert.ToDouble(configuration["SliderCPU"]); + SliderMemory = Convert.ToDouble(configuration["SliderMemory"]); + SliderDisk = Convert.ToDouble(configuration["SliderDisk"]); + + SecCPU = Convert.ToDouble(configuration["SecCPU"]); + SecMemory = Convert.ToDouble(configuration["SecMemory"]); + SecDisk = Convert.ToDouble(configuration["SecDisk"]); + } + catch (Exception) { string messageBoxText = "Please try to set this app expect on secur app"; string caption = "Failed to get setting"; @@ -77,13 +84,6 @@ namespace BusynessNotification DiskSecTextBox.Text = SecDisk.ToString(); } - private void CPUSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) - { - int CPUvalue = (int)e.NewValue; - TextBox_CPUSlider.Text = CPUvalue.ToString(); - CPUSlider.Value = CPUvalue; - } - private void TextBox_CPUSlider_TextChanged(object sender, TextChangedEventArgs e) { TextBox_CPUSlider.Text = InputNormalizerToDouble(TextBox_CPUSlider.Text,SliderCPU).ToString(); @@ -93,7 +93,17 @@ namespace BusynessNotification { TextBox_MemorySlider.Text = InputNormalizerToDouble(TextBox_MemorySlider.Text,SliderMemory).ToString(); } + private void TextBox_DiskSlider_TextChanged(object sender, TextChangedEventArgs e) + { + TextBox_CPUSlider.Text = InputNormalizerToDouble(TextBox_CPUSlider.Text.ToString(), SliderDisk).ToString(); + } + private void CPUSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) + { + int CPUvalue = (int)e.NewValue; + TextBox_CPUSlider.Text = CPUvalue.ToString(); + CPUSlider.Value = CPUvalue; + } private void MemorySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { int Memoryvalue = (int)e.NewValue; @@ -108,11 +118,6 @@ namespace BusynessNotification DiskSlider.Value = Diskvalue; } - private void TextBox_DiskSlider_TextChanged(object sender, TextChangedEventArgs e) - { - TextBox_CPUSlider.Text = InputNormalizerToDouble(TextBox_CPUSlider.Text.ToString(), SliderDisk).ToString(); - } - private void CPUSecTextBox_TextChanged(object sender, TextChangedEventArgs e) { CPUSecTextBox.Text = InputNormalizerToDouble(CPUSecTextBox.Text,SecCPU).ToString(); @@ -132,20 +137,22 @@ namespace BusynessNotification { try { - Properties.Settings.Default.CheckCPU = CPUcheck.IsChecked.Value; - Properties.Settings.Default.CheckMemory = Memorycheck.IsChecked.Value; - Properties.Settings.Default.CheckDisk = Diskcheck.IsChecked.Value; - - Properties.Settings.Default.SliderCPU = CPUSlider.Value; - Properties.Settings.Default.SliderMemory = MemorySlider.Value; - Properties.Settings.Default.SliderDisk = DiskSlider.Value; - - Properties.Settings.Default.SecCPU = int.Parse(CPUSecTextBox.Text.ToString()); - Properties.Settings.Default.SecMemory = int.Parse(MemorySecTextBox.Text.ToString()); - Properties.Settings.Default.SecDisk = int.Parse(DiskSecTextBox.Text.ToString()); - - Properties.Settings.Default.Save(); - }catch (Exception) + SettingJson data = new SettingJson(); + + data.CheckCPU = true; + data.CheckMemory = true; + data.CheckDisk = true; + data.SliderCPU = 50; + data.SliderMemory = 50; + data.SliderDisk = 50; + data.SecCPU = 5; + data.SecMemory = 5; + data.SecDisk = 5; + + string jsonStr = JsonSerializer.Serialize(data); + Debug.WriteLine(jsonStr); + } + catch (Exception) { string messageBoxText = "Please try to set this app expect on secur app"; string caption = "Failed to get setting"; @@ -171,9 +178,9 @@ namespace BusynessNotification { double value; bool NormalizationCan = double.TryParse(input, out value); - if(NormalizationCan == false || value < 0 || value >= 1000) + if(NormalizationCan == false || value <= 0 || value >= 100) { - string messageBoxText = "Please enter up to 3 digits?"; + string messageBoxText = "Please enter up to 100"; string caption = "unexpected input"; MessageBoxButton button = MessageBoxButton.OK; MessageBoxImage icon = MessageBoxImage.Warning; diff --git a/ObserveUseage.cs b/ObserveUseage.cs @@ -1,4 +1,5 @@ -using Microsoft.Toolkit.Uwp.Notifications; +using Microsoft.Extensions.Configuration; +using Microsoft.Toolkit.Uwp.Notifications; using Microsoft.VisualBasic.ApplicationServices; using System; using System.Collections.Generic; @@ -16,19 +17,22 @@ namespace BusynessNotification public async void Controller() { + var configuration = new ConfigurationBuilder() + .AddJsonFile("SettingJson.json") + .Build(); Debug.WriteLine("Controller has colled"); ///設定から数値を取得 - bool CheckCPU = Properties.Settings.Default.CheckCPU; - bool CheckMemory = Properties.Settings.Default.CheckMemory; - bool CheckDisk = Properties.Settings.Default.CheckDisk; + bool CheckCPU = Convert.ToBoolean(configuration["CheckCPU"]); + bool CheckMemory = Convert.ToBoolean(configuration["CheckMemory"]); + bool CheckDisk = Convert.ToBoolean(configuration["CheckDisk"]); - double CPUSlider = Properties.Settings.Default.SliderCPU; - double MemorySlider = Properties.Settings.Default.SliderMemory; - double DiskSlider = Properties.Settings.Default.SliderDisk; + double CPUSlider = Convert.ToDouble(configuration["SliderCPU"]); + double MemorySlider = Convert.ToDouble(configuration["SliderMemory"]); + double DiskSlider = Convert.ToDouble(configuration["SliderDisk"]); - int SecCPU = Properties.Settings.Default.SecCPU; - int SecMemory = Properties.Settings.Default.SecMemory; - int SecDisk = Properties.Settings.Default.SecDisk; + double SecCPU = Convert.ToDouble(configuration["SecCPU"]); + double SecMemory = Convert.ToDouble(configuration["SecMemory"]); + double SecDisk = Convert.ToDouble(configuration["SecDisk"]); int flagCPU = 0; int flagMemory = 0; diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs @@ -1,134 +0,0 @@ -//------------------------------------------------------------------------------ -// <auto-generated> -// このコードはツールによって生成されました。 -// ランタイム バージョン:4.0.30319.42000 -// -// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 -// コードが再生成されるときに損失したりします。 -// </auto-generated> -//------------------------------------------------------------------------------ - -namespace BusynessNotification.Properties { - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.9.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default { - get { - return defaultInstance; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("True")] - public bool CheckCPU { - get { - return ((bool)(this["CheckCPU"])); - } - set { - this["CheckCPU"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("True")] - public bool CheckMemory { - get { - return ((bool)(this["CheckMemory"])); - } - set { - this["CheckMemory"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("True")] - public bool CheckDisk { - get { - return ((bool)(this["CheckDisk"])); - } - set { - this["CheckDisk"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("50")] - public double SliderCPU { - get { - return ((double)(this["SliderCPU"])); - } - set { - this["SliderCPU"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("90")] - public double SliderMemory { - get { - return ((double)(this["SliderMemory"])); - } - set { - this["SliderMemory"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("50")] - public double SliderDisk { - get { - return ((double)(this["SliderDisk"])); - } - set { - this["SliderDisk"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("10")] - public int SecCPU { - get { - return ((int)(this["SecCPU"])); - } - set { - this["SecCPU"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("10")] - public int SecMemory { - get { - return ((int)(this["SecMemory"])); - } - set { - this["SecMemory"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("10")] - public int SecDisk { - get { - return ((int)(this["SecDisk"])); - } - set { - this["SecDisk"] = value; - } - } - } -} diff --git a/Properties/Settings.settings b/Properties/Settings.settings @@ -1,33 +0,0 @@ -<?xml version='1.0' encoding='utf-8'?> -<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="BusynessNotification.Properties" GeneratedClassName="Settings"> - <Profiles /> - <Settings> - <Setting Name="CheckCPU" Type="System.Boolean" Scope="User"> - <Value Profile="(Default)">True</Value> - </Setting> - <Setting Name="CheckMemory" Type="System.Boolean" Scope="User"> - <Value Profile="(Default)">True</Value> - </Setting> - <Setting Name="CheckDisk" Type="System.Boolean" Scope="User"> - <Value Profile="(Default)">True</Value> - </Setting> - <Setting Name="SliderCPU" Type="System.Double" Scope="User"> - <Value Profile="(Default)">50</Value> - </Setting> - <Setting Name="SliderMemory" Type="System.Double" Scope="User"> - <Value Profile="(Default)">90</Value> - </Setting> - <Setting Name="SliderDisk" Type="System.Double" Scope="User"> - <Value Profile="(Default)">50</Value> - </Setting> - <Setting Name="SecCPU" Type="System.Int32" Scope="User"> - <Value Profile="(Default)">10</Value> - </Setting> - <Setting Name="SecMemory" Type="System.Int32" Scope="User"> - <Value Profile="(Default)">10</Value> - </Setting> - <Setting Name="SecDisk" Type="System.Int32" Scope="User"> - <Value Profile="(Default)">10</Value> - </Setting> - </Settings> -</SettingsFile>- \ No newline at end of file diff --git a/SettingJson.cs b/SettingJson.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BusynessNotification +{ + class SettingJson + { + public bool CheckCPU { get; set; } + public bool CheckMemory { get; set; } + public bool CheckDisk { get; set; } + public double SliderCPU { get; set; } + public double SliderMemory { get; set; } + public double SliderDisk { get; set; } + public double SecCPU { get; set; } + public double SecMemory { get; set; } + public double SecDisk { get; set; } + } +} diff --git a/SettingJson.json b/SettingJson.json @@ -0,0 +1,11 @@ +{ + "CheckCPU": "true", + "CheckMemory": "true", + "CheckDisk": "true", + "SliderCPU": "50", + "SliderMemory": "50", + "SliderDisk": "50", + "SecCPU": "5", + "SecMemory": "5", + "SecDisk": "5" +}+ \ No newline at end of file