using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace WatchDogWindowApp.WPF { public class ListsAndGridsViewModel : INotifyPropertyChanged { string InitialDirectory = ""; string ExePath = ""; public ListsAndGridsViewModel() { GetLocalData(); StartAllCommand = new AnotherCommandImplementation(_ => { foreach (var exe in ExeCollection) { exe.IsAutoStart = true; } }); KillAllCommand = new AnotherCommandImplementation(_ => { foreach (var exe in ExeCollection) { exe.IsAutoStart = false; KillProcess(exe.Name); } }); RemoveAllCommand = new AnotherCommandImplementation(_ => { foreach (var exe in ExeCollection) { KillProcess(exe.Name); } ExeCollection.Clear(); SetLocalData(); }); AddCommand = new AnotherCommandImplementation(_ => { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = InitialDirectory;//注意这里写路径时要用c:\\而不是c:\ openFileDialog.Filter = "可执行文件|*.exe"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if ((bool)openFileDialog.ShowDialog()) { var fName = openFileDialog.FileName; InitialDirectory = System.IO.Path.GetDirectoryName(fName); ExePath = System.IO.Path.GetFullPath(fName); string name = System.IO.Path.GetFileName(ExePath); var model = new SelectableViewModel() { Code = name.First(), Description = InitialDirectory, FileName = ExePath, Name = name.Trim(".exe".ToCharArray()), }; if (!ExeCollection.Contains(model)) { ExeCollection.Add(model); } } SetLocalData(); }); RemoveCommand = new AnotherCommandImplementation(_ => { foreach (var exe in ExeCollection) { if (exe.IsSelected) { KillProcess(exe.Name); ExeCollection.Remove(exe); break; } } SetLocalData(); }); StartCommand = new AnotherCommandImplementation(_ => { foreach (var exe in ExeCollection) { if (exe.IsSelected) { exe.IsAutoStart = true; } } }); ReStartCommand = new AnotherCommandImplementation(_=> { foreach (var exe in ExeCollection) { if (exe.IsSelected) { exe.IsAutoStart = false; StartProcess(exe.FileName, exe.Description, exe.Name); exe.IsAutoStart = true; } } }); CloseCommand = new AnotherCommandImplementation(_=> { foreach (var exe in ExeCollection) { if (exe.IsSelected) { exe.IsAutoStart = false; KillProcess(exe.Name); } } }); Task.Factory.StartNew(() => { while (true) { ExceuteAutoStartExe(); Task.Delay(1000).Wait(); } }); } private void GetLocalData() { string path =Path.GetFullPath( "./cfg.json"); if (File.Exists(path)) { try { FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream); var json = streamReader.ReadToEnd(); fileStream.Close(); streamReader.Close(); var models = JsonConvert.DeserializeObject>(json); if (models != null) { foreach (var item in models) { ExeCollection.Add(item); } } } catch (Exception ex) { File.Delete(path); throw ex; } } } private void SetLocalData() { string path = Path.GetFullPath("./cfg.json"); if (!File.Exists(path)) { FileStream fileStream1 = File.Create(path); fileStream1.Close(); } var json = JsonConvert.SerializeObject(ExeCollection); FileStream fileStream = new FileStream(path, FileMode.Truncate, FileAccess.Write); var bytes = Encoding.UTF8.GetBytes(json); fileStream.Seek(0, SeekOrigin.Begin); fileStream.Write(bytes); fileStream.Flush(); fileStream.Close(); } private void ExceuteAutoStartExe() { foreach (var exe in ExeCollection) { if (exe.IsAutoStart) { var processes = Process.GetProcessesByName(exe.Name); if (processes != null && processes.Length > 0) { } else { StartProcess(exe.FileName, exe.Description, exe.Name); } } } } private void StartProcess(string fileName,string directory,string name) { var processes = Process.GetProcessesByName(name); if (processes != null && processes.Length > 0) { foreach (var item in processes) { item.EnableRaisingEvents = false; //item.Exited -= Pro_Exited; item.Kill(); } } ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = fileName; psi.WorkingDirectory = directory; var pro = Process.Start(psi); //pro.EnableRaisingEvents = true; //pro.Exited += this.Pro_Exited; } //private void Pro_Exited(object sender, System.EventArgs e) //{ // Process process = sender as Process; // if (process!=null) // { // StartProcess(process.StartInfo.FileName,process.StartInfo.WorkingDirectory); // } //} private void KillProcess(string name) { var pro = Process.GetProcessesByName(name); if (pro != null && pro.Length > 0) { foreach (var item in pro) { item.EnableRaisingEvents = false; //item.Exited -= Pro_Exited; item.Kill(); } } } public ICommand StartAllCommand { get; } public ICommand StartCommand { get; } public ICommand KillAllCommand { get; } public ICommand RemoveAllCommand { get; } public ICommand AddCommand { get; } public ICommand RemoveCommand { get; } public ICommand ReStartCommand { get; } public ICommand CloseCommand { get; } public ObservableCollection ExeCollection { get; } = new ObservableCollection(); public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }