SourceInsight是个看代码的好工具;在平时的工作中真是离不开,可是它的license也很贵,人民币好像要2K多吧,在公司用的时候不敢乱用sn,只好用试用版的,它的试用版是从安装起开始30天的期限,到期后如果把系统时间改回去还可以继续使用。以前都是每次启动SI之前先把系统时间调回去,启动完毕之后再调回来,并且启动完毕后还要点击一下try。。。每天都要这样来一次,挺麻烦的,所以就做了一个小工具,这个小工具首先修改系统时间,然后启动SI,启动完成后自动点击try,然后恢复系统时间,这样,点击一次就可以正常使用SI了:)
没有几行代码,用C#写的,里面主用了这几个方面的东西:
1.c#中如何修改系统时间;
2.c#中如何根据标题找窗口句柄;
3.c#中如何通过窗口句柄向该窗口发送快捷键;
4.c#中如何启动外部程序;
5.c#中如何让线程睡眠;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace SI
- {
- public partial class SI : Form
- {
- SYSTEMTIME oldst;
- SYSTEMTIME newst;
- public SI()
- {
- InitializeComponent();
- SYSTEMTIME oldst = new SYSTEMTIME();
- SYSTEMTIME newst = new SYSTEMTIME();
- start();
- }
- public struct SYSTEMTIME
- {
- public ushort wYear;
- public ushort wMonth;
- public ushort wDayOfWeek;
- public ushort wDay;
- public ushort wHour;
- public ushort wMinute;
- public ushort wSecond;
- public ushort wMilliseconds;
- /// Convert form System.DateTime
- public void FromDateTime(DateTime time)
- {
- wYear = (ushort)time.Year;
- wMonth = (ushort)time.Month;
- wDayOfWeek = (ushort)time.DayOfWeek;
- wDay = (ushort)time.Day;
- wHour = (ushort)time.Hour;
- wMinute = (ushort)time.Minute;
- wSecond = (ushort)time.Second;
- wMilliseconds = (ushort)time.Millisecond;
- }
- }
- //SetLocalTime C# Signature
- [DllImport("Kernel32.dll")]
- public static extern bool SetLocalTime(ref SYSTEMTIME Time);
- /// <summary >
- /// 获取窗口标题的句柄
- /// </summary >
- /// <param name= "类名 " >指定类名,不指定可以输入Null </param >
- /// <param name= "程序标题 " >查找窗体的标题名称 </param >
- /// <returns > </returns >
- [DllImport("user32.dll")]
- public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
- [DllImport("user32.dll")]
- public static extern bool SetForegroundWindow(IntPtr hWnd);
- //[DllImport( "user32.dll ",EntryPoint= " ")]
- /// <summary >
- /// 应用程序的主入口点。
- /// </summary >
- [STAThread]
- private void start()
- {
- //获取系统当前时间,并保存起来以便恢复
- DateTime oldt = DateTime.Now;
- oldst.FromDateTime(oldt);
- //设置SI可以运行的时间2008-1-1
- newst.wYear = 2008;
- newst.wMonth = 1;
- newst.wDay = 1;
- SetLocalTime(ref newst);
- //准备启动SI
- System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
- //设置外部程序名
- Info.FileName = "Insight3.exe";
- //设置外部程序的启动参数(命令行参数)为test.txt
- //Info.Arguments = "test.txt";
- //设置外部程序工作目录,这里可以考虑可以配置
- Info.WorkingDirectory = "d://program files//Source Insight 3";
- //声明一个程序类
- System.Diagnostics.Process Proc;
- try
- {
- //启动外部程序
- Proc = System.Diagnostics.Process.Start(Info);
- //等待source insight启动完成,知道出来试用期的对话框
- IntPtr hwnd;
- do
- {
- Thread.Sleep(100);
- //窗体title可以考虑可配置
- hwnd = FindWindow(null, "Source Insight - 28 Days Left in Trial License");
- }
- while (hwnd == IntPtr.Zero);
- //把试用期对话框置前,然后向其发送快捷键Alt + t,关闭对话框
- SetForegroundWindow(hwnd);
- SendKeys.SendWait("%t");
- //恢复系统时间,然后程序退出
- SetLocalTime(ref oldst);
- System.Environment.Exit(0);
- }
- catch (System.ComponentModel.Win32Exception error)
- {
- MessageBox.Show("系统找不到指定的程序文件");
- System.Environment.Exit(0);
- }
- }
- }
- }