using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Reflection;
using System.Windows;
using UtilLib;
namespace OpenTapEditor;
public class GlobalConfig
{
public double GlobalFontSize { get; set; } = 14;
}
public class GlobalConfigHolder
{
public static GlobalConfig Config { get; internal set; }
}
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
private void WriteRegister()
{
string Company = "JingCe", Program = "SequenceEditor";
try
{
string registryPath = @$"SOFTWARE\{Company}\{Program}";
using RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath, true)
?? throw new Exception("无法创建或打开注册表键");
string path = AppDomain.CurrentDomain.BaseDirectory;
key.SetValue("Location", path, RegistryValueKind.String);
}
catch (Exception ex)
{
//log.Error("Write Register Failed.", ex);
}
}
public static Process GetProcess()
{
Process cur = Process.GetCurrentProcess();
string exePath = Assembly.GetExecutingAssembly().Location;
string query = $"SELECT ProcessId FROM Win32_Process WHERE ExecutablePath = '{exePath.Replace("\\", "\\\\")}'";
using (var searcher = new ManagementObjectSearcher(query))
{
foreach (var obj in searcher.Get())
{
int processId = Convert.ToInt32(obj["ProcessId"]);
if (processId != cur.Id)
{
return Process.GetProcessById(processId);
}
}
}
return null;
}
private void LoadConfig()
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GlobalSetting.xml");
if (!File.Exists(path))
{
GlobalConfigHolder.Config = new GlobalConfig();
}
try
{
GlobalConfig config = XmlHelper.DeserializeFromXml(path);
GlobalConfigHolder.Config = config;
}
catch (Exception ex)
{
GlobalConfigHolder.Config = new GlobalConfig();
}
this.Resources["GlobalFontSize"] = GlobalConfigHolder.Config.GlobalFontSize;
}
private Mutex mutex;
private void Application_Startup(object sender, StartupEventArgs e)
{
var process = GetProcess();
mutex = new Mutex(true, "OpenTapEditor_OnlyRun_CRNS");
if (mutex.WaitOne(0, false))//&& process == null
{
WriteRegister();
LoadConfig();
//LoadingWindow window = new LoadingWindow();
MainWindow window = new MainWindow();
window.Show();
}
else
{
MessageBox.Show("程序已经在运行!", "提示");
this.Shutdown();
}
}
}