將寫死改成活讀
This commit is contained in:
@@ -1,70 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace CMM.Library.Base
|
||||
namespace CMM.Library.Base;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class ObservableRangeCollection<T> : ObservableCollection<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
|
||||
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class ObservableRangeCollection<T> : ObservableCollection<T>
|
||||
public void AddRange(IEnumerable<T> collection)
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
|
||||
/// </summary>
|
||||
public void AddRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException("collection");
|
||||
if (collection == null) throw new ArgumentNullException("collection");
|
||||
|
||||
foreach (var i in collection) Items.Add(i);
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
|
||||
/// </summary>
|
||||
public void RemoveRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException("collection");
|
||||
|
||||
foreach (var i in collection) Items.Remove(i);
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current collection and replaces it with the specified item.
|
||||
/// </summary>
|
||||
public void Replace(T item)
|
||||
{
|
||||
ReplaceRange(new T[] { item });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current collection and replaces it with the specified collection.
|
||||
/// </summary>
|
||||
public void ReplaceRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException("collection");
|
||||
|
||||
Items.Clear();
|
||||
foreach (var i in collection) Items.Add(i);
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
|
||||
/// </summary>
|
||||
public ObservableRangeCollection()
|
||||
: base() { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
|
||||
/// </summary>
|
||||
/// <param name="collection">collection: The collection from which the elements are copied.</param>
|
||||
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
|
||||
public ObservableRangeCollection(IEnumerable<T> collection)
|
||||
: base(collection) { }
|
||||
foreach (var i in collection) Items.Add(i);
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
|
||||
/// </summary>
|
||||
public void RemoveRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException("collection");
|
||||
|
||||
foreach (var i in collection) Items.Remove(i);
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current collection and replaces it with the specified item.
|
||||
/// </summary>
|
||||
public void Replace(T item)
|
||||
{
|
||||
ReplaceRange(new T[] { item });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current collection and replaces it with the specified collection.
|
||||
/// </summary>
|
||||
public void ReplaceRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException("collection");
|
||||
|
||||
Items.Clear();
|
||||
foreach (var i in collection) Items.Add(i);
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
|
||||
/// </summary>
|
||||
public ObservableRangeCollection()
|
||||
: base() { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
|
||||
/// </summary>
|
||||
/// <param name="collection">collection: The collection from which the elements are copied.</param>
|
||||
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
|
||||
public ObservableRangeCollection(IEnumerable<T> collection)
|
||||
: base(collection) { }
|
||||
}
|
||||
|
||||
@@ -1,31 +1,30 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace CMM.Library.Base
|
||||
namespace CMM.Library.Base;
|
||||
|
||||
public class PropertyBase : INotifyPropertyChanged
|
||||
{
|
||||
public class PropertyBase : INotifyPropertyChanged
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
virtual internal protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
virtual internal protected void OnPropertyChanged(string propertyName)
|
||||
if (this.PropertyChanged != null)
|
||||
{
|
||||
if (this.PropertyChanged != null)
|
||||
{
|
||||
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
protected void SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
||||
{
|
||||
if (object.Equals(storage, value)) return;
|
||||
|
||||
storage = value;
|
||||
this.OnPropertyChanged(propertyName);
|
||||
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
protected void SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
||||
{
|
||||
if (object.Equals(storage, value)) return;
|
||||
|
||||
storage = value;
|
||||
this.OnPropertyChanged(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,75 +2,69 @@
|
||||
using CMM.Library.Base;
|
||||
using CMM.Library.Helpers;
|
||||
using CMM.Library.Method;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace CMM.Library.Config
|
||||
namespace CMM.Library.Config;
|
||||
|
||||
public class XConfig : PropertyBase
|
||||
{
|
||||
public class XConfig : PropertyBase
|
||||
[JsonIgnore]
|
||||
public string Version { get; private set; }
|
||||
|
||||
public static string ConfigFileName => Path.Combine(AssemblyData.Path, "Config.cfg");
|
||||
|
||||
#region Language
|
||||
[JsonIgnore]
|
||||
public CultureInfo Culture
|
||||
{
|
||||
[JsonIgnore]
|
||||
public string Version { get; private set; }
|
||||
|
||||
public static string ConfigFileName => Path.Combine(AssemblyData.Path, "Config.cfg");
|
||||
|
||||
#region Language
|
||||
[JsonIgnore]
|
||||
public CultureInfo Culture
|
||||
get => _Culture;
|
||||
set
|
||||
{
|
||||
get => _Culture;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _Culture, value);
|
||||
LoadCultures();
|
||||
}
|
||||
SetProperty(ref _Culture, value);
|
||||
LoadCultures();
|
||||
}
|
||||
CultureInfo _Culture;
|
||||
public string Language { get; set; } = null;
|
||||
CulturesHelper CulturesHelper { get; init; } = new();
|
||||
public void LoadCultures()
|
||||
{
|
||||
if (CulturesHelper == null) return;
|
||||
}
|
||||
CultureInfo _Culture;
|
||||
public string Language { get; set; } = null;
|
||||
CulturesHelper CulturesHelper { get; init; } = new();
|
||||
public void LoadCultures()
|
||||
{
|
||||
if (CulturesHelper == null) return;
|
||||
|
||||
CulturesHelper.ChangeCulture(Culture);
|
||||
}
|
||||
#endregion
|
||||
CulturesHelper.ChangeCulture(Culture);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual void Load()
|
||||
{
|
||||
XConfig _base = null;
|
||||
if (new FileInfo(ConfigFileName).Exists)
|
||||
{
|
||||
try
|
||||
{
|
||||
_base = ConfigFileName.JsonFormFile<XConfig>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"{Lang.Find("LoadConfigErr")}{ex.Message}", "failed", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
this.Culture = _base?.Culture ?? new CultureInfo(_base?.Language ?? "en-US", false);
|
||||
this.Version = $"{AssemblyData.AppName} {AssemblyData.AppVersion}";
|
||||
}
|
||||
|
||||
public virtual void Save()
|
||||
public virtual void Load()
|
||||
{
|
||||
XConfig _base = null;
|
||||
if (new FileInfo(ConfigFileName).Exists)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.FileToJson(ConfigFileName);
|
||||
_base = ConfigFileName.JsonFormFile<XConfig>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"{Lang.Find("SaveConfigErr")}{ex.Message}", "failed", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
MessageBox.Show($"{Lang.Find("LoadConfigErr")}{ex.Message}", "failed", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
this.Culture = _base?.Culture ?? new CultureInfo(_base?.Language ?? "en-US", false);
|
||||
this.Version = $"{AssemblyData.AppName} {AssemblyData.AppVersion}";
|
||||
}
|
||||
|
||||
public virtual void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.FileToJson(ConfigFileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"{Lang.Find("SaveConfigErr")}{ex.Message}", "failed", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace CMM.Library.Helpers
|
||||
namespace CMM.Library.Helpers;
|
||||
|
||||
internal class ConsoleHelper
|
||||
{
|
||||
internal class ConsoleHelper
|
||||
const string cmdFileName = "cmd.exe";
|
||||
private static Process CreatProcess(string fileName) =>
|
||||
new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = fileName,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
|
||||
public static async Task<string> ExecuteCommand(string command)
|
||||
{
|
||||
const string cmdFileName = "cmd.exe";
|
||||
private static Process CreatProcess(string fileName) =>
|
||||
new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = fileName,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
Process p = new Process();
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.StartInfo.CreateNoWindow = true;
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
p.StartInfo.FileName = command;
|
||||
p.Start();
|
||||
var output = await p.StandardOutput.ReadToEndAsync();
|
||||
await p.WaitForExitAsync();
|
||||
|
||||
public static async Task<string> CmdCommandAsync(params string[] cmds) =>
|
||||
await CommandAsync(cmdFileName, cmds);
|
||||
return output;
|
||||
}
|
||||
|
||||
public static string CmdCommand(params string[] cmds) =>
|
||||
Command(cmdFileName, cmds);
|
||||
public static async Task<string> CmdCommandAsync(params string[] cmds) =>
|
||||
await CommandAsync(cmdFileName, cmds);
|
||||
|
||||
public static async Task<string> CommandAsync(string fileName, params string[] cmds)
|
||||
public static string CmdCommand(params string[] cmds) =>
|
||||
Command(cmdFileName, cmds);
|
||||
|
||||
public static async Task<string> CommandAsync(string fileName, params string[] cmds)
|
||||
{
|
||||
var p = CreatProcess(fileName);
|
||||
p.Start();
|
||||
foreach (var cmd in cmds)
|
||||
{
|
||||
var p = CreatProcess(fileName);
|
||||
p.Start();
|
||||
foreach (var cmd in cmds)
|
||||
{
|
||||
p.StandardInput.WriteLine(cmd);
|
||||
}
|
||||
p.StandardInput.WriteLine("exit");
|
||||
var result = await p.StandardOutput.ReadToEndAsync();
|
||||
var error = await p.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(error))
|
||||
result = result + "\r\n<Error Message>:\r\n" + error;
|
||||
await p.WaitForExitAsync();
|
||||
p.Close();
|
||||
Debug.WriteLine(result);
|
||||
return result;
|
||||
p.StandardInput.WriteLine(cmd);
|
||||
}
|
||||
p.StandardInput.WriteLine("exit");
|
||||
var result = await p.StandardOutput.ReadToEndAsync();
|
||||
var error = await p.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(error))
|
||||
result = result + "\r\n<Error Message>:\r\n" + error;
|
||||
await p.WaitForExitAsync();
|
||||
p.Close();
|
||||
Debug.WriteLine(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string Command(string fileName, params string[] cmds)
|
||||
public static string Command(string fileName, params string[] cmds)
|
||||
{
|
||||
var p = CreatProcess(fileName);
|
||||
p.Start();
|
||||
foreach (var cmd in cmds)
|
||||
{
|
||||
var p = CreatProcess(fileName);
|
||||
p.Start();
|
||||
foreach (var cmd in cmds)
|
||||
{
|
||||
p.StandardInput.WriteLine(cmd);
|
||||
}
|
||||
p.StandardInput.WriteLine("exit");
|
||||
var result = p.StandardOutput.ReadToEnd();
|
||||
var error = p.StandardError.ReadToEnd();
|
||||
if (!string.IsNullOrWhiteSpace(error))
|
||||
result = result + "\r\n<Error Message>:\r\n" + error;
|
||||
p.WaitForExit();
|
||||
p.Close();
|
||||
Debug.WriteLine(result);
|
||||
return result;
|
||||
p.StandardInput.WriteLine(cmd);
|
||||
}
|
||||
p.StandardInput.WriteLine("exit");
|
||||
var result = p.StandardOutput.ReadToEnd();
|
||||
var error = p.StandardError.ReadToEnd();
|
||||
if (!string.IsNullOrWhiteSpace(error))
|
||||
result = result + "\r\n<Error Message>:\r\n" + error;
|
||||
p.WaitForExit();
|
||||
p.Close();
|
||||
Debug.WriteLine(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CMM.Library.Helpers
|
||||
namespace CMM.Library.Helpers;
|
||||
|
||||
static class JsonSerializerExtensions
|
||||
{
|
||||
static class JsonSerializerExtensions
|
||||
public static JsonSerializerOptions defaultSettings = new JsonSerializerOptions()
|
||||
{
|
||||
public static JsonSerializerOptions defaultSettings = new JsonSerializerOptions()
|
||||
WriteIndented = true,
|
||||
IgnoreNullValues = true,
|
||||
PropertyNamingPolicy = null,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
};
|
||||
}
|
||||
|
||||
public static class JsonHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 複製整個obj全部結構
|
||||
/// </summary>
|
||||
public static T DeepCopy<T>(T RealObject) =>
|
||||
JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(RealObject, JsonSerializerExtensions.defaultSettings));
|
||||
|
||||
public static string JsonFormResource(this string fileName)
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var resourceName =
|
||||
assembly.GetManifestResourceNames().
|
||||
Where(str => str.Contains(fileName)).FirstOrDefault();
|
||||
if (resourceName == null) return "";
|
||||
|
||||
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
||||
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
WriteIndented = true,
|
||||
IgnoreNullValues = true,
|
||||
PropertyNamingPolicy = null,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
};
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
internal static class JsonHelper
|
||||
public static T JsonFormResource<T>(this string fileName) =>
|
||||
JsonFormString<T>(JsonFormResource(fileName));
|
||||
|
||||
public static T JsonFormFile<T>(this string fileName) =>
|
||||
JsonFormString<T>(Load(fileName));
|
||||
|
||||
public static T JsonFormString<T>(this string json) =>
|
||||
JsonSerializer.Deserialize<T>(json);
|
||||
|
||||
public static void FileToJson<T>(this T payload, string savePath) =>
|
||||
Save(savePath, payload.ToJson());
|
||||
|
||||
public static string ToJson<T>(this T payload) =>
|
||||
JsonSerializer.Serialize(payload, JsonSerializerExtensions.defaultSettings);
|
||||
|
||||
/// <summary>
|
||||
/// 從Embedded resource讀string
|
||||
/// </summary>
|
||||
/// <param name="aFileName">resource位置,不含副檔名</param>
|
||||
public static string GetResource(this Assembly assembly, string aFileName)
|
||||
{
|
||||
/// <summary>
|
||||
/// 複製整個obj全部結構
|
||||
/// </summary>
|
||||
public static T DeepCopy<T>(T RealObject) =>
|
||||
JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(RealObject, JsonSerializerExtensions.defaultSettings));
|
||||
var resourceName = assembly
|
||||
.GetManifestResourceNames()
|
||||
.Where(str => str.Contains(aFileName))
|
||||
.FirstOrDefault();
|
||||
if (resourceName == null) return "";
|
||||
|
||||
public static string JsonFormResource(this string fileName)
|
||||
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
||||
using (var sr = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var resourceName =
|
||||
assembly.GetManifestResourceNames().
|
||||
Where(str => str.Contains(fileName)).FirstOrDefault();
|
||||
if (resourceName == null) return "";
|
||||
|
||||
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
||||
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public static T JsonFormResource<T>(this string fileName) =>
|
||||
JsonFormString<T>(JsonFormResource(fileName));
|
||||
public static string Load(string aFileName) =>
|
||||
Load(new FileInfo(aFileName));
|
||||
|
||||
public static T JsonFormFile<T>(this string fileName) =>
|
||||
JsonFormString<T>(Load(fileName));
|
||||
|
||||
public static T JsonFormString<T>(this string json) =>
|
||||
JsonSerializer.Deserialize<T>(json);
|
||||
|
||||
public static void FileToJson<T>(this T payload, string savePath) =>
|
||||
Save(savePath, payload.ToJson());
|
||||
|
||||
public static string ToJson<T>(this T payload) =>
|
||||
JsonSerializer.Serialize(payload, JsonSerializerExtensions.defaultSettings);
|
||||
|
||||
/// <summary>
|
||||
/// 從Embedded resource讀string
|
||||
/// </summary>
|
||||
/// <param name="aFileName">resource位置,不含副檔名</param>
|
||||
public static string GetResource(this Assembly assembly, string aFileName)
|
||||
public static string Load(FileInfo aFi)
|
||||
{
|
||||
if (aFi.Exists)
|
||||
{
|
||||
var resourceName = assembly
|
||||
.GetManifestResourceNames()
|
||||
.Where(str => str.Contains(aFileName))
|
||||
.FirstOrDefault();
|
||||
if (resourceName == null) return "";
|
||||
|
||||
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
||||
using (var sr = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public static string Load(string aFileName) =>
|
||||
Load(new FileInfo(aFileName));
|
||||
|
||||
public static string Load(FileInfo aFi)
|
||||
{
|
||||
if (aFi.Exists)
|
||||
{
|
||||
string _Json = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
var sr = new StreamReader(aFi.FullName);
|
||||
_Json = sr.ReadToEnd();
|
||||
sr.Close();
|
||||
}
|
||||
catch (IOException) { throw; }
|
||||
catch (Exception) { throw; }
|
||||
|
||||
return _Json;
|
||||
}
|
||||
|
||||
throw new Exception("開檔失敗。");
|
||||
}
|
||||
|
||||
public static void Save(string filePath, string content) =>
|
||||
Save(new FileInfo(filePath), content);
|
||||
|
||||
public static void Save(FileInfo aFi, string aContent)
|
||||
{
|
||||
if (!aFi.Directory.Exists)
|
||||
{
|
||||
aFi.Directory.Create();
|
||||
}
|
||||
|
||||
if (aFi.Exists)
|
||||
{
|
||||
aFi.Delete();
|
||||
}
|
||||
|
||||
aFi.Refresh();
|
||||
if (aFi.Exists) throw new Exception("寫檔失敗,檔案已存在或已開啟。");
|
||||
string _Json = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
File.WriteAllText(aFi.FullName, aContent);
|
||||
var sr = new StreamReader(aFi.FullName);
|
||||
_Json = sr.ReadToEnd();
|
||||
sr.Close();
|
||||
}
|
||||
catch (IOException) { throw; }
|
||||
catch (Exception) { throw; }
|
||||
|
||||
return _Json;
|
||||
}
|
||||
|
||||
throw new Exception("開檔失敗。");
|
||||
}
|
||||
|
||||
public static void Save(string filePath, string content) =>
|
||||
Save(new FileInfo(filePath), content);
|
||||
|
||||
public static void Save(FileInfo aFi, string aContent)
|
||||
{
|
||||
if (!aFi.Directory.Exists)
|
||||
{
|
||||
aFi.Directory.Create();
|
||||
}
|
||||
|
||||
if (aFi.Exists)
|
||||
{
|
||||
aFi.Delete();
|
||||
}
|
||||
|
||||
aFi.Refresh();
|
||||
if (aFi.Exists) throw new Exception("寫檔失敗,檔案已存在或已開啟。");
|
||||
|
||||
try
|
||||
{
|
||||
File.WriteAllText(aFi.FullName, aContent);
|
||||
}
|
||||
catch (IOException) { throw; }
|
||||
catch (Exception) { throw; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
using System;
|
||||
using System.Security.Principal;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace CMM.Library.Helpers
|
||||
namespace CMM.Library.Helpers;
|
||||
|
||||
public class UAC
|
||||
{
|
||||
public class UAC
|
||||
public static void Check()
|
||||
{
|
||||
public static void Check()
|
||||
{
|
||||
var identity = WindowsIdentity.GetCurrent();
|
||||
var principal = new WindowsPrincipal(identity);
|
||||
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
|
||||
throw new Exception($"Cannot delete task with your current identity '{identity.Name}' permissions level." +
|
||||
"You likely need to run this application 'as administrator' even if you are using an administrator account.");
|
||||
var identity = WindowsIdentity.GetCurrent();
|
||||
var principal = new WindowsPrincipal(identity);
|
||||
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
|
||||
throw new Exception($"Cannot delete task with your current identity '{identity.Name}' permissions level." +
|
||||
"You likely need to run this application 'as administrator' even if you are using an administrator account.");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<TargetFramework>net7.0-windows</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>CMM.Library</RootNamespace>
|
||||
<AssemblyName>CMM.Library</AssemblyName>
|
||||
<Product>ControlMyMonitorManagement</Product>
|
||||
<UseWPF>true</UseWPF>
|
||||
<Company>Dang</Company>
|
||||
@@ -34,7 +35,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CMMModel\CMMModel.csproj" />
|
||||
<ProjectReference Include="..\Language\Language.csproj" />
|
||||
<ProjectReference Include="..\Tester\Tester.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,48 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace CMM.Library.Method
|
||||
namespace CMM.Library.Method;
|
||||
|
||||
public static class AssemblyData
|
||||
{
|
||||
public static class AssemblyData
|
||||
/// <summary>
|
||||
/// 當下Assembly名稱
|
||||
/// </summary>
|
||||
public static string AssemblyName => System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
||||
|
||||
public static string AppName => AppDomain.CurrentDomain.FriendlyName;
|
||||
|
||||
/// <summary>
|
||||
/// 程式根目錄,無視工作目錄
|
||||
/// </summary>
|
||||
public static string Path => AppDomain.CurrentDomain.BaseDirectory;
|
||||
/// <summary>
|
||||
/// 版本
|
||||
/// </summary>
|
||||
public static string AssemblyVersion => GetAssemblyVersion();
|
||||
public static string AppVersion => GetFileVersion(Process.GetCurrentProcess().MainModule.FileName);
|
||||
/// <summary>
|
||||
/// CCM 輸出
|
||||
/// </summary>
|
||||
public static string smonitors => System.IO.Path.Combine(Path, "smonitors.tmp");
|
||||
|
||||
static string GetAssemblyVersion()
|
||||
{
|
||||
/// <summary>
|
||||
/// 當下Assembly名稱
|
||||
/// </summary>
|
||||
public static string AssemblyName => System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
||||
var fi = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace(@"file:///", "");
|
||||
return GetFileVersion(fi);
|
||||
}
|
||||
|
||||
public static string AppName => AppDomain.CurrentDomain.FriendlyName;
|
||||
|
||||
/// <summary>
|
||||
/// 程式根目錄,無視工作目錄
|
||||
/// </summary>
|
||||
public static string Path => AppDomain.CurrentDomain.BaseDirectory;
|
||||
/// <summary>
|
||||
/// 版本
|
||||
/// </summary>
|
||||
public static string AssemblyVersion => GetAssemblyVersion();
|
||||
public static string AppVersion => GetFileVersion(Process.GetCurrentProcess().MainModule.FileName);
|
||||
/// <summary>
|
||||
/// CCM 輸出
|
||||
/// </summary>
|
||||
public static string smonitors => System.IO.Path.Combine(Path, "smonitors.tmp");
|
||||
|
||||
static string GetAssemblyVersion()
|
||||
{
|
||||
var fi = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace(@"file:///", "");
|
||||
return GetFileVersion(fi);
|
||||
}
|
||||
|
||||
public static string GetFileVersion(string filePath)
|
||||
{
|
||||
var fvi = FileVersionInfo.GetVersionInfo(filePath);
|
||||
return $"{fvi.FileMajorPart}." +
|
||||
$"{fvi.FileMinorPart}." +
|
||||
$"{fvi.FileBuildPart}." +
|
||||
$"{fvi.FilePrivatePart}";
|
||||
}
|
||||
public static string GetFileVersion(string filePath)
|
||||
{
|
||||
var fvi = FileVersionInfo.GetVersionInfo(filePath);
|
||||
return $"{fvi.FileMajorPart}." +
|
||||
$"{fvi.FileMinorPart}." +
|
||||
$"{fvi.FileBuildPart}." +
|
||||
$"{fvi.FilePrivatePart}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
using CMM.Library.Helpers;
|
||||
using CMM.Library.ViewModel;
|
||||
using System.IO;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Threading;
|
||||
|
||||
namespace CMM.Library.Method;
|
||||
|
||||
@@ -13,7 +15,6 @@ public static class CMMCommand
|
||||
static readonly string CMMTmpFolder = Path.Combine(Path.GetTempPath(), $"CMM");
|
||||
static readonly string CMMexe = Path.Combine(CMMTmpFolder, "ControlMyMonitor.exe");
|
||||
static readonly string CMMsMonitors = Path.Combine(CMMTmpFolder, "smonitors.tmp");
|
||||
static readonly string CMMcfg = Path.Combine(CMMTmpFolder, "ControlMyMonitor.cfg");
|
||||
|
||||
public static async Task ScanMonitor()
|
||||
{
|
||||
@@ -21,80 +22,92 @@ public static class CMMCommand
|
||||
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /smonitors {CMMsMonitors}");
|
||||
}
|
||||
|
||||
public static async Task PowerOn(string MonitorSn)
|
||||
public static Task PowerOn(string monitorSN)
|
||||
{
|
||||
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {MonitorSn} D6 1");
|
||||
return ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {monitorSN} D6 1");
|
||||
}
|
||||
|
||||
public static async Task Sleep(string MonitorSn)
|
||||
public static Task Sleep(string monitorSN)
|
||||
{
|
||||
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {MonitorSn} D6 4");
|
||||
return ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {monitorSN} D6 4");
|
||||
}
|
||||
|
||||
public static async Task ScanMonitorInterfaces(IEnumerable<XMonitor> monitors)
|
||||
private static async Task<string> GetMonitorValue(string monitorSN)
|
||||
{
|
||||
var taskList = new List<Task>();
|
||||
foreach (var mon in monitors)
|
||||
var cmdFileName = Path.Combine(CMMTmpFolder, $"{Guid.NewGuid()}.bat");
|
||||
var cmd = $"{CMMexe} /GetValue {monitorSN} D6\r\n" +
|
||||
$"echo %errorlevel%";
|
||||
File.WriteAllText(cmdFileName, cmd);
|
||||
var values = await ConsoleHelper.ExecuteCommand(cmdFileName);
|
||||
File.Delete(cmdFileName);
|
||||
return values.Split("\r\n", StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
|
||||
}
|
||||
|
||||
public static async Task<string> GetMonPowerStatus(string monitorSN)
|
||||
{
|
||||
var status = await GetMonitorValue(monitorSN);
|
||||
|
||||
return status switch
|
||||
{
|
||||
taskList.Add(Task.Run(async () => await
|
||||
ScanMonitorInterfaces($"{CMMTmpFolder}\\{mon.SerialNumber}.tmp", mon)));
|
||||
}
|
||||
|
||||
await Task.WhenAll(taskList.ToArray());
|
||||
"1" => "PowerOn",
|
||||
"4" => "Sleep",
|
||||
"5" => "PowerOff",
|
||||
_ => string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
static async Task ScanMonitorInterfaces(string savePath, XMonitor mon)
|
||||
public static async Task ScanMonitorStatus(IEnumerable<XMonitor> monitors)
|
||||
{
|
||||
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /scomma {savePath} {mon.MonitorID}");
|
||||
await mon.ReadMonitorStatus(savePath);
|
||||
var taskList = monitors.Select(x =>
|
||||
{
|
||||
return ScanMonitorStatus($"{CMMTmpFolder}\\{x.SerialNumber}.tmp", x);
|
||||
});
|
||||
|
||||
await Task.WhenAll(taskList);
|
||||
}
|
||||
|
||||
static async Task ScanMonitorStatus(string savePath, XMonitor mon)
|
||||
{
|
||||
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /sjson {savePath} {mon.MonitorID}");
|
||||
var monitorModel = JsonHelper.JsonFormFile<IEnumerable<SMonitorModel>>(savePath);
|
||||
|
||||
var status = monitorModel.ReadMonitorStatus();
|
||||
|
||||
mon.Status = new ObservableRangeCollection<XMonitorStatus>(status);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取得螢幕狀態
|
||||
/// </summary>
|
||||
public static async Task ReadMonitorStatus(this XMonitor @this, string filePath)
|
||||
public static IEnumerable<XMonitorStatus> ReadMonitorStatus(this IEnumerable<SMonitorModel> monitorModel)
|
||||
{
|
||||
var statusColle = new ObservableRangeCollection<XMonitorStatus>();
|
||||
|
||||
if (!File.Exists(filePath)) return;
|
||||
|
||||
foreach (var line in await File.ReadAllLinesAsync(filePath))
|
||||
{
|
||||
var sp = line.Split(",");
|
||||
if (sp.Length < 6) continue;
|
||||
|
||||
statusColle.Add(new XMonitorStatus
|
||||
foreach (var m in monitorModel)
|
||||
{
|
||||
yield return new XMonitorStatus
|
||||
{
|
||||
VCP_Code = StrTrim(sp[0]),
|
||||
VCPCodeName = StrTrim(sp[1]),
|
||||
Read_Write = StrTrim(sp[2]),
|
||||
CurrentValue = TryGetInt(sp[3]),
|
||||
MaximumValue = TryGetInt(sp[4]),
|
||||
PossibleValues = TryGetArrStr(sp),
|
||||
});
|
||||
VCP_Code = m.VCPCode,
|
||||
VCPCodeName = m.VCPCodeName,
|
||||
Read_Write = m.ReadWrite,
|
||||
CurrentValue = TryGetInt(m.CurrentValue),
|
||||
MaximumValue = TryGetInt(m.MaximumValue),
|
||||
PossibleValues = TryGetArrStr(m.PossibleValues),
|
||||
};
|
||||
}
|
||||
|
||||
@this.Status = statusColle;
|
||||
|
||||
string StrTrim(string str)
|
||||
IEnumerable<int> TryGetArrStr(string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str)) return null;
|
||||
return str;
|
||||
}
|
||||
|
||||
string TryGetArrStr(string[] strArr)
|
||||
{
|
||||
if (strArr.Length < 7) return null;
|
||||
|
||||
var outStr = string.Join(",", strArr[5..]);
|
||||
outStr = outStr.Substring(1, outStr.Length - 2);
|
||||
return outStr;
|
||||
return str.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(x => TryGetInt(x))
|
||||
.Where(x => x != null)
|
||||
.Select(x => (int)x)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
int? TryGetInt(string str)
|
||||
{
|
||||
if (int.TryParse(str, out var value)) return value;
|
||||
return null;
|
||||
return int.TryParse(str, out var value)
|
||||
? value
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +1,23 @@
|
||||
using CMM.Library.Base;
|
||||
using CMM.Library.ViewModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CMM.Library.Method
|
||||
namespace CMM.Library.Method;
|
||||
|
||||
public class CMMMgr : PropertyBase
|
||||
{
|
||||
public class CMMMgr : PropertyBase
|
||||
public ObservableRangeCollection<XMonitor> Monitors
|
||||
{
|
||||
public ObservableRangeCollection<XMonitor> Monitors
|
||||
{
|
||||
get => _Monitors;
|
||||
set { SetProperty(ref _Monitors, value); }
|
||||
}
|
||||
ObservableRangeCollection<XMonitor> _Monitors = new ();
|
||||
get => _Monitors;
|
||||
set { SetProperty(ref _Monitors, value); }
|
||||
}
|
||||
ObservableRangeCollection<XMonitor> _Monitors = new ();
|
||||
|
||||
public async Task Init()
|
||||
{
|
||||
await CMMCommand.ScanMonitor();
|
||||
var monColle = new ObservableRangeCollection<XMonitor>();
|
||||
monColle.AddRange(await CMMCommand.ReadMonitorsData());
|
||||
Monitors = monColle;
|
||||
await CMMCommand.ScanMonitorInterfaces(monColle);
|
||||
}
|
||||
public async Task Init()
|
||||
{
|
||||
await CMMCommand.ScanMonitor();
|
||||
var monColle = new ObservableRangeCollection<XMonitor>();
|
||||
monColle.AddRange(await CMMCommand.ReadMonitorsData());
|
||||
Monitors = monColle;
|
||||
await CMMCommand.ScanMonitorStatus(monColle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +1,66 @@
|
||||
using CMM.Library.Base;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CMM.Library.ViewModel
|
||||
namespace CMM.Library.ViewModel;
|
||||
|
||||
public class XMonitor : PropertyBase
|
||||
{
|
||||
public class XMonitor : PropertyBase
|
||||
/// <summary>
|
||||
/// 裝置路徑
|
||||
/// </summary>
|
||||
public string MonitorDeviceName
|
||||
{
|
||||
/// <summary>
|
||||
/// 裝置路徑
|
||||
/// </summary>
|
||||
public string MonitorDeviceName
|
||||
{
|
||||
get => _MonitorDeviceName;
|
||||
set { SetProperty(ref _MonitorDeviceName, value); }
|
||||
}
|
||||
string _MonitorDeviceName;
|
||||
|
||||
/// <summary>
|
||||
/// 裝置名稱
|
||||
/// </summary>
|
||||
public string MonitorName
|
||||
{
|
||||
get => _MonitorName;
|
||||
set { SetProperty(ref _MonitorName, value); }
|
||||
}
|
||||
string _MonitorName;
|
||||
|
||||
/// <summary>
|
||||
/// 裝置序號
|
||||
/// </summary>
|
||||
public string SerialNumber
|
||||
{
|
||||
get => _SerialNumber;
|
||||
set { SetProperty(ref _SerialNumber, value); }
|
||||
}
|
||||
string _SerialNumber;
|
||||
|
||||
/// <summary>
|
||||
/// 訊號裝置
|
||||
/// </summary>
|
||||
public string AdapterName
|
||||
{
|
||||
get => _AdapterName;
|
||||
set { SetProperty(ref _AdapterName, value); }
|
||||
}
|
||||
string _AdapterName;
|
||||
|
||||
/// <summary>
|
||||
/// 裝置識別碼
|
||||
/// </summary>
|
||||
public string MonitorID
|
||||
{
|
||||
get => _MonitorID;
|
||||
set { SetProperty(ref _MonitorID, value); }
|
||||
}
|
||||
string _MonitorID;
|
||||
|
||||
/// <summary>
|
||||
/// 狀態
|
||||
/// </summary>
|
||||
public ObservableRangeCollection<XMonitorStatus> Status
|
||||
{
|
||||
get => _Status;
|
||||
set { SetProperty(ref _Status, value); }
|
||||
}
|
||||
ObservableRangeCollection<XMonitorStatus> _Status = new();
|
||||
get => _MonitorDeviceName;
|
||||
set { SetProperty(ref _MonitorDeviceName, value); }
|
||||
}
|
||||
string _MonitorDeviceName;
|
||||
|
||||
/// <summary>
|
||||
/// 裝置名稱
|
||||
/// </summary>
|
||||
public string MonitorName
|
||||
{
|
||||
get => _MonitorName;
|
||||
set { SetProperty(ref _MonitorName, value); }
|
||||
}
|
||||
string _MonitorName;
|
||||
|
||||
/// <summary>
|
||||
/// 裝置序號
|
||||
/// </summary>
|
||||
public string SerialNumber
|
||||
{
|
||||
get => _SerialNumber;
|
||||
set { SetProperty(ref _SerialNumber, value); }
|
||||
}
|
||||
string _SerialNumber;
|
||||
|
||||
/// <summary>
|
||||
/// 訊號裝置
|
||||
/// </summary>
|
||||
public string AdapterName
|
||||
{
|
||||
get => _AdapterName;
|
||||
set { SetProperty(ref _AdapterName, value); }
|
||||
}
|
||||
string _AdapterName;
|
||||
|
||||
/// <summary>
|
||||
/// 裝置識別碼
|
||||
/// </summary>
|
||||
public string MonitorID
|
||||
{
|
||||
get => _MonitorID;
|
||||
set { SetProperty(ref _MonitorID, value); }
|
||||
}
|
||||
string _MonitorID;
|
||||
|
||||
/// <summary>
|
||||
/// 狀態
|
||||
/// </summary>
|
||||
public ObservableRangeCollection<XMonitorStatus> Status
|
||||
{
|
||||
get => _Status;
|
||||
set { SetProperty(ref _Status, value); }
|
||||
}
|
||||
ObservableRangeCollection<XMonitorStatus> _Status = new();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
using CMM.Library.Base;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CMM.Library.ViewModel
|
||||
{
|
||||
@@ -44,11 +39,11 @@ namespace CMM.Library.ViewModel
|
||||
}
|
||||
int? _MaximumValue;
|
||||
|
||||
public string PossibleValues
|
||||
public IEnumerable<int> PossibleValues
|
||||
{
|
||||
get => _PossibleValues;
|
||||
set { SetProperty(ref _PossibleValues, value); }
|
||||
}
|
||||
string _PossibleValues;
|
||||
IEnumerable<int> _PossibleValues;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user