这是自己写的一个wpf小程序,想要一个邀请码,谢谢。本来有看雪号的,但是忘记账号信息了,囧啊
标 题: 定时扫描启动程序,用配置文件保存设置
作 者: qqykx
时 间: 2009年12月23日
链 接: http://bbs.pediy.com/showthread.php?t=103674
【文章标题】: 定时扫描启动程序,用配置文件保存设置
【文章作者】: qqykx
【作者邮箱】: 413779714@qq.com
【编写语言】: VC#
【使用工具】: VS.NET 2008 SP1
【操作平台】: Windows 7
【作者声明】: 软件可以任意修改和传播
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Win32;
namespace Start_Application
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void StartAppWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
if (!File.Exists(configtxt))
{
File.AppendAllText(configtxt, "", Encoding.Default);
}
string quanbuneirong = File.ReadAllText(configtxt);
string[] quan = quanbuneirong.Split('*');
foreach (string peizhixiangmu in quan)
{
if ((peizhixiangmu != null) && (peizhixiangmu != ""))
{
listBox1.Items.Add(peizhixiangmu);
}
}
listBox1.SelectedIndex = 0;
}
catch { MessageBox.Show("读取程序列表失败"); }
}
DispatcherTimer tm = new DispatcherTimer();
private void Start_Click(object sender, RoutedEventArgs e)
{
if (listBox1.Items.Count == 0)
{
MessageBox.Show("请先添加文件!");
}
if (listBox1.Items.Count > 0)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入查询时间");
textBox1.Focus();
}
else
{
tm.Tick += new EventHandler(dingshi);
tm.Interval = TimeSpan.FromSeconds(0.1);
tm.Start();
label2.Content = "打开";
}
}
}
private void dingshi(object sender, EventArgs e)
{
string label2content = Convert.ToString(label2.Content);
if (label2content == "打开")
{
int jiange = Convert.ToInt32(textBox1.Text);
tm.Interval = TimeSpan.FromSeconds(jiange);
tm.Tick += new EventHandler(kong);
string app = Convert.ToString(listBox1.SelectedItem);
string appname = System.IO.Path.GetFileNameWithoutExtension(app);
int ProgressCount = 0;
Process[] prc = Process.GetProcesses();
foreach (Process pr in prc)
{
string pname = pr.ProcessName;
if (appname == pname)
{
ProgressCount += 1;
}
}
if (ProgressCount == 0)
{
//try
{ Process.Start(app); }
//catch { MessageBox.Show("进程相关的出问题了!"); }
}
}
}
private void kong(object sender, EventArgs e)
{
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
if (listBox1.Items.Count == 0)
{
MessageBox.Show("请先添加文件!");
}
if (listBox1.Items.Count > 0)
{
label2.Content = "关闭";
}
}
private void AddApp_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "打开要定时检查的程序";
openFileDialog1.Filter = "可执行文件 (*.exe)|*.exe";
openFileDialog1.InitialDirectory = @"F:\program Files\";
openFileDialog1.FileName = "";
Nullable<bool> result = openFileDialog1.ShowDialog();
if (result == true)
{
string firstopenitem = openFileDialog1.FileName;
xiepeizhi(firstopenitem);
}
}
string configtxt = Directory.GetCurrentDirectory() + @"\q.txt";
private void xiepeizhi(string gangdakai)
{
try
{
bool itemexist = true;
string quanbuneirong = File.ReadAllText(configtxt);
string[] quan = quanbuneirong.Split('*');
foreach (string configitem in quan)
{
if (configitem == gangdakai)
{
itemexist = false;
}
}
if (itemexist == true)
{
ListBoxItem i = new ListBoxItem();
i.Content = gangdakai;
int index = listBox1.Items.Add(i);
listBox1.SelectedIndex = index;
File.AppendAllText(configtxt, gangdakai + "*");
}
}
catch { MessageBox.Show("加条目写文件失败了!"); }
}
private void TopMostCheck_Click(object sender, RoutedEventArgs e)
{
if (TopMostCheck.IsChecked == true)
{
StartAppWindow.Topmost = true;
}
else
{
StartAppWindow.Topmost = false;
}
}
private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
try
{
string itemzhi = listBox1.SelectedItem.ToString();
StreamReader sr = new StreamReader(configtxt, Encoding.Default);
string context = sr.ReadToEnd();
sr.Close();
context = context.Replace(itemzhi + "*", "");
StreamWriter sw = new StreamWriter(configtxt);
sw.Write(context);
sw.Close();
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
listBox1.SelectedIndex = 0;
}
catch { MessageBox.Show("删除启动条目失败!"); }
}
private void listBox1_MouseEnter(object sender, MouseEventArgs e)
{
if ((listBox1.SelectedIndex != -1) && (listBox1.Items.Count > 0))
{
ContextMenu cms = new ContextMenu();
cms.FontSize = 20;
MenuItem cmsitem = new MenuItem();
cmsitem.Header = ("删除该选项");
cmsitem.Click += new RoutedEventHandler(MenuItemDelete_Click);
cms.Items.Add(cmsitem);
listBox1.ContextMenu = cms;
}
else
{
listBox1.ContextMenu = null;
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox;
if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
{
if (txt.Text.Contains(".") && e.Key == Key.Decimal)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else if ((e.Key >= Key.D0 && e.Key <= Key.D9)
&& e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
}
string checkZero = txt.Text;
string zero = "0";
if (checkZero.IndexOf(zero,0) == 0)
{
txt.SelectAll();
}
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, 0);
int offset = change[0].Offset;
if (change[0].AddedLength > 0)
{
double num = 0;
if (!Double.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
textBox.Select(offset, 0);
}
}
}
private void StartAppWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left) this.DragMove();
}
private void WindowsClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)